Warm tip: This article is reproduced from serverfault.com, please click

Getting the absolute path of the executable, using C#?

发布于 2009-11-01 22:01:00

Have a look at this pseudocode:

string exe_path = system.get_exe_path()
print "This executable is located in " + exe_path

If I build the above program and place the executable in C:/meow/, It would print out This executable is located in C:/meow/ each time it is run, regardless of the current working directory.

How could I easily accomplish this using C#?

Questioner
user12163
Viewed
0
Mark Rushakoff 2009-11-02 06:03:32

MSDN has an article that says to use System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase; if you need the directory, use System.IO.Path.GetDirectoryName on that result.

Or, there's the shorter Application.ExecutablePath which "Gets the path for the executable file that started the application, including the executable name" so that might mean it's slightly less reliable depending on how the application was launched.