如何:获取应用程序目录

更新: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 中生成应用程序和核心任务