Learn how to retrieve the executable path of a C# Console based application.

Normally an application should be able to locate other resources using relative paths, without needing that the user select some files manually in order to work properly. However, in some cases third party libraries need absolute paths to work as they should, so you will need to fulfill this requirement. Obviously, we are talking about internal functionalities, so the user shouldn't know nothing about this, so you can build paths starting from the directory that you currently have in your application.

In this short article, we'll share with you a simple snippet to know the current path of the executable of your C# console based application.

A. Absolute path with executable

If you are willing to retrieve the absolute path of your executable (including the .exe to the path), you may want to use the Assembly class located in the System.Reflection namespace. The System.Reflection namespace contains types that retrieve information about assemblies, modules, members, parameters, and other entities in managed code by examining their metadata. These types also can be used to manipulate instances of loaded types, for example to hook up events or to invoke methods. The Assembly class represents by itself an assembly (:v), which is a reusable, versionable, and self-describing building block of a common language runtime application.

You can retrieve the absolute path to the executable with the following instruction:

using System;
using System.Reflection;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Retrieve the absolute path of the current executable.
            string path = Assembly.GetEntryAssembly().Location;

            // Prints something like:
            // C:\Users\sdkca\ConsoleApp1\ConsoleApp1\bin\Debug\ConsoleApp1.exe
            Console.WriteLine(path);

            // Pause application to read the output in the console
            Console.ReadLine();
        }
    }
}

B. Absolute path without executable

If you want instead to retrieve the base directory of the executable, you don't need to use the Reflection namespace, instead just simply access the BaseDirectory from the application domain for the current Thread:

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Retrieve the absolute path of the current executable.
            string path = AppDomain.CurrentDomain.BaseDirectory;

            // Prints something like:
            // C:\Users\sdkca\ConsoleApp1\ConsoleApp1\bin\Debug\
            Console.WriteLine(path);

            // Pause application to read the output in the console
            Console.ReadLine();
        }
    }
}

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors