开发者

Getting the Installation Folder of Running Application using C#

There were several other similar posts, but I could not 开发者_运维知识库pinpoint one that relates entirely to my issue.

Simply put, say my application exe file is located in C:\MyApp\run.exe,

how can I find the programatically find the path C:\MyApp


using System.IO;
using System.Windows.Forms;

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

UPDATE:

For a WPF application you could use the following:

using System.Reflection;

string appPath = Assembly.GetExecutingAssembly().Location;


Two of the answers given are correct, but rely on using Windows Forms. If that is not your cup of tea, there are alternatives.

Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)

and also

Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)


You can try Environment.CurrentDirectory - if your program hasn't manipulated this value for any reason, it should show you the path from which the program was run.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜