HOW TO:取得應用程式目錄

更新:2007 年 11 月

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

範例

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

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))
        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));
                                                                                                                                

編譯程式碼

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

請參閱

其他資源

Pocket PC 開發和 .NET Compact Framework

在 .NET Compact Framework 中建置應用程式和核心工作