PROWAREtech

articles » current » dot-net » executable-path

.NET: Retrieve the Executable's Path and Directory

How to find the .NET executable's path and directory using the C# programming language.

Find the executable's path and its directory by using Environment.ProcessPath. This will return the path to the executable.

// NOTE: Get the full name path of the executable file
string exeFullName = Environment.ProcessPath;
// NOTE: Get the exe's directory
string dirFullName = System.IO.Path.GetDirectoryName(exeFullName); // NOTE: this will be the directory of the executable which will not be IWebHostEnvironment.ContentRootPath when debugging in Visual Studio

Alternatively, use System.Reflection.Assembly.GetExecutingAssembly().Location:

// NOTE: Get the full name path of the executable file
string exeFullName = System.Reflection.Assembly.GetExecutingAssembly().Location;
// NOTE: Get the exe's directory
string dirFullName = System.IO.Path.GetDirectoryName(exeFullName); // NOTE: this will be the directory of the executable which will not be IWebHostEnvironment.ContentRootPath when debugging in Visual Studio

Now use System.IO.Path.Combine() to work in the executable's directory regardless of the platform.


PROWAREtech

Hello there! How can I help you today?
Ask any question

PROWAREtech

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
ACCEPT REJECT