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

C#DirectoryExecutable

C# Problem Overview


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#?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

AppDomain.CurrentDomain.BaseDirectory

Solution 3 - C#

using System.Reflection;

string myExeDir = new FileInfo(Assembly.GetEntryAssembly().Location).Directory.ToString();

Solution 4 - C#

var dir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

I jumped in for the top rated answer and found myself not getting what I expected. I had to read the comments to find what I was looking for.

For that reason I am posting the answer listed in the comments to give it the exposure it deserves.

Solution 5 - C#

"Gets the path or UNC location of the loaded file that contains the manifest."

See: http://msdn.microsoft.com/en-us/library/system.reflection.assembly.location.aspx

Application.ResourceAssembly.Location

Solution 6 - C#

Suppose i have .config file in console app and now am getting like below.

Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\YourFolderName\\log4net.config";

Solution 7 - C#

On my side, I used, with a form application:

String Directory = System.Windows.Forms.Application.StartupPath;

it takes the application startup path.

Solution 8 - C#

The one that worked for me and isn't above was Process.GetCurrentProcess().MainModule.FileName.

Solution 9 - C#

If you are planning to build a console application to be used with Task Scheduler, I'd recommend using this approach:

var execDirectoryPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)?.Replace("file:\\", "");

This way, the path will adapt to whatever location you place your executable file in.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser12163View Question on Stackoverflow
Solution 1 - C#Mark RushakoffView Answer on Stackoverflow
Solution 2 - C#Liquid CoreView Answer on Stackoverflow
Solution 3 - C#newbView Answer on Stackoverflow
Solution 4 - C#User1View Answer on Stackoverflow
Solution 5 - C#Mike de KlerkView Answer on Stackoverflow
Solution 6 - C#Muhammad MubashirView Answer on Stackoverflow
Solution 7 - C#SébastienView Answer on Stackoverflow
Solution 8 - C#Charles A.View Answer on Stackoverflow
Solution 9 - C#Joseph L.View Answer on Stackoverflow