按一下以給予評分及指教

  開啟低頻寬檢視
本頁僅適用於
Microsoft Visual Studio 2008/.NET Framework 3.5

其他版本也適用於下列軟體:
.NET Framework 開發人員手冊
HOW TO:取得應用程式目錄

更新:2007 年 11 月

因為在 Pocket PC 應用程式中沒有固有的目前目錄設定,所以在程式碼中指定檔案名稱而未指定路徑時就會傳回 FileNotFoundException。Pocket PC 應用程式會將資料檔和組件檔儲存在 \Program Files\myAssembly\ 中,其中 myAssembly 為組件名稱。

這個範例顯示如何透過取得執行中組件的完整目錄名稱並附加應用程式的檔案名稱,來判斷目前執行中應用程式的路徑。請注意,若應用程式正於裝置的根目錄中執行,則傳回的路徑資訊會是空字串。

Visual Basic
Dim strAppDir As String = Path.GetDirectoryName( _
    Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim strFullPathToMyFile As String = Path.Combine(strAppDir, "myFileName.txt")

MessageBox.Show(String.Format("Path to the application is: '{0}'." + _
    "Full path to the file in the application folder is: '{1}'", _
    strAppDir, strFullPathToMyFile))

C#
        String strAppDir = Path.GetDirectoryName(
            Assembly.GetExecutingAssembly().GetName().CodeBase);
        String strFullPathToMyFile = Path.Combine(strAppDir, "fileName.txt");

        MessageBox.Show(String.Format("Path to the application is: '{0}'." +
            "Full path to the file in the application folder is: '{1}'",
            strAppDir, strFullPathToMyFile));
                                                                                                                                   

這個範例需要下列命名空間的參考:

社群內容   什麼是社群內容?
新增內容 RSS  註解
Processing
© 2009 Microsoft Corporation. 著作權所有,並保留一切權利。 使用規定  |  商標  |  隱私權聲明
Page view tracker