How to retrieve the Executable Path in C#

By FoxLearn 7/3/2024 9:30:22 AM   137
In C#, you can retrieve the executable path of the current application using the System.Reflection namespace.

Here’s how to retrieve the Executable Path in C#

// c# get the full path to the currently executing assembly
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
Console.WriteLine("Executable Path: " + exePath);

Use the System.Reflection.Assembly.GetExecutingAssembly() method to retrieve the assembly that contains the code that is currently executing.

Using .Location gives you the full path to the location of the assembly file.

If you want to retrieve the base directory of the executable without using the Reflection namespace, you can directly access it through the AppDomain class.

// c# get the base directory of the executable
string baseDir = AppDomain.CurrentDomain.BaseDirectory;        
Console.WriteLine("Base Directory: " + baseDir);

Using AppDomain.CurrentDomain.BaseDirectory gives you the base directory where the executable is located. It's often preferred over using System.Reflection.Assembly.GetExecutingAssembly().Location when all you need is the base directory of the executable .