Skip to content

API Proposal: get full path of current process executable #40862

@jkotas

Description

@jkotas

Background and Motivation

The path of the current process executable is often needed for logging or to find more files next to the current process executable.

This API is needed more than before now that we support single-file publishing and assemblies do not have physical file paths anymore. See https://github.com/dotnet/designs/blob/master/accepted/2020/form-factors.md#single-file for details.

We have internal APIs to get current process executable path in this repo that return current executable path, e.g. here: https://github.com/dotnet/runtime/search?q=GetExePath&unscoped_q=GetExePath

Proposed API

namespace System
{
    public partial class Environment
    {
        // Returns the path to the file that launched the process. For framework dependent apps
        // this will return the path to dotnet.exe. For environment where this concept doesn't
        // exist, for example WebAssembly, the method will return null. 
        public string? ProcessPath => (pseudo) Process.GetCurrentProcess().MainModule.FileName;

        // Returns the directory path of the application.
        public string? AppBaseDirectory { get; }
    }
}

Usage Examples

Console.WriteLine(Environment.ProcessExecutablePath);

string logFilePath = Path.Combine(Environment.AppBaseDirectory, "logfile.txt");

Examples of equivalent APIs in other environments:

Alternative Designs

The current cross-platform way to get full path of current process is Process.GetCurrentProcess().MainModule.FileName. This workaround is very inefficient because of it does a lot more than what is required to get the current process path. More discussion on this issue is in #13051 .

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions