简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM