Skip to main content

Get the full path of running process in C#.

public static string GetProcessPath()
{
    // fix the path so that if running under the debugger we get the original file.
    string processName = Process.GetCurrentProcess().MainModule.FileName;
    int index = processName.IndexOf("vshost", StringComparison.Ordinal);
    if (index != -1)
    {
        string first = processName.Substring(0, index);
        int numChars = processName.Length - (index + 7);
        string second = processName.Substring(index + 7, numChars);
        processName = first + second;
    }

    return processName;
}

// Option 2:
// console program's can use:
private static string AppPath => Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);