如何获取应用程序的路径(没有app.exe)?

我想得到我的应用程序的路径:“\\ ProgramFiles \\ myApp”,我尝试使用以下代码:

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

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

我也尝试过:

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

但它会抛出“NotSupportedException”。

有没有办法在没有.exe的情况下获得一条路径?

 path = System.IO.Path.GetDirectoryName( path ); 

Application.StartupPath应该为您做到这一点。

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

 private static string GetApplicationPath() { return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); } 

比其他更简单:

 using System.IO; using System.Reflection; ... var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) 

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

 Path.GetFileNameWithoutExtension(path); 

如果它是你的情况下的exe使用:

  // Summary: // Gets the path for the executable file that started the // application, not including the executable name. Application.StartupPath 

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

在Vb.Net中:

 fi = New FileInfo(System.Reflection.Assembly.GetExecutingAssembly.Location) path = fi.DirectoryName