How to get the path of app(without app.exe)?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 02:22:10

问题


I want to get the path of my app like: "\\ProgramFiles\\myApp", I try to use the following code:


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

But it returns a path which has "\\myapp.exe" at the end.

I also tried:


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

But it throws an “NotSupportedException”.

Is there any way to get a path without .exe at the end?


回答1:


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



回答2:


Application.StartupPath should do that for you.

Update: from you edit I see that you are running on Compact Framework. Then Application.StartupPath will not work. This is the construct that I usually use then:

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



回答3:


More simple than the rest:

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



回答4:


You can use Path.GetDirectoryName(string) method passing your original path string as parameter for this. What problem are you trying to solve? Maybe you really need something like working directory?




回答5:


Path.GetFileNameWithoutExtension(path);



回答6:


If its an exe as in your case use:

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



回答7:


What about using a FileInfo object to extract the directory name?

In Vb.Net:

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


来源:https://stackoverflow.com/questions/881251/how-to-get-the-path-of-appwithout-app-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!