8

我想获取我的应用程序的路径,例如:“\\ProgramFiles\\myApp”,我尝试使用以下代码:


string path = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;

但它返回一个结尾有“\\myapp.exe”的路径。

我也试过:


string path = System.IO.Directory.GetCurrentDirectory();

但它会引发“NotSupportedException”。

有没有办法在最后获得没有 .exe 的路径?

4

7 回答 7

19

Application.StartupPath应该为你做。

更新:从您的编辑中,我看到您正在 Compact Framework 上运行。然后 Application.StartupPath 将不起作用。这是我通常使用的构造:

private static string GetApplicationPath()
{
    return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}
于 2009-05-19T06:35:02.793 回答
11
path = System.IO.Path.GetDirectoryName( path );
于 2009-05-19T06:34:40.250 回答
3

比其他的更简单:

using System.IO;
using System.Reflection;
...
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
于 2009-05-19T06:58:57.013 回答
1

您可以使用 Path.GetDirectoryName(string) 方法将原始路径字符串作为参数传递。你想解决什么问题?也许你真的需要像工作目录这样的东西?

于 2009-05-19T06:36:29.590 回答
0
Path.GetFileNameWithoutExtension(path);
于 2009-05-19T06:36:31.167 回答
0

使用 FileInfo 对象提取目录名称怎么样?

在 Vb.Net 中:

fi = New FileInfo(System.Reflection.Assembly.GetExecutingAssembly.Location)
path = fi.DirectoryName
于 2009-05-19T06:36:57.827 回答
0

如果它是您的情况下的 exe,请使用:

    // Summary:
    //     Gets the path for the executable file that started the 
    //     application, not including the executable name.    
    Application.StartupPath
于 2009-05-19T06:56:04.490 回答