User Tools

Site Tools


get_path_exe_dotnet

Get Path For Executable (.NET/C#)

To get the path that a .NET console executable resides in, regardless of where the executable was launched from:

var exePath = Path.GetDirectoryName(Environment.ProcessPath ?? "")

This is useful in a scenario where you want to access a file that resides in the same directory, e.g., a configuration file:

IConfiguration config = new ConfigurationBuilder()
    .AddJsonFile(Path.Join(Path.GetDirectoryName(Environment.ProcessPath ?? ""), "my-app-config.json"))
    .AddEnvironmentVariables()
    .Build();
get_path_exe_dotnet.txt · Last modified: by jimc