How to Get the Executing Exe's Path in .Net

What is the best way to get the executing exe's path in .NET?

I usually access the directory that contains my application's .exe with:

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

get path for my .exe

System.Reflection.Assembly.GetEntryAssembly().Location;

Getting the path of the executable file's current location

It isn't hard.

But it MIGHT depend on your target platform (which you haven't specified).

For .Net Core 1.x and .Net 5 or higher, I would use AppContext.BaseDirectory

Here are some other alternatives for various environments over the years:

6 ways to get the current directory in C#, August 17,
2010

  • AppDomain.CurrentDomain.BaseDirectory
    This is the best option all round. It will give you the base directory for class libraries,
    including those in ASP.NET applications.

  • Directory.GetCurrentDirectory()
    Note: in .NET Core this is the current best practice. The details below relate to the .NET Framework
    4.5 and below.

    This does an interop call using the winapi GetCurrentDirectory call
    inside kernel32.dll, which means the launching process’ folder will
    often be returned. Also as the MSDN documents say, it’s not guaranteed
    to work on mobile devices.

  • Environment.CurrentDirectory

    This simply calls Directory.GetCurrentDirectory()

  • Assembly.Location

    This would be called using

    this.GetType().Assembly.Location

    This returns the full path to the calling assembly, including the
    assembly name itself. If you are calling a separate class library,
    then its base directory will be returned, such “C:\myassembly.dll” -
    depending obviously on which Assembly instance is being used.

  • Application.StartupPath

    This is inside the System.Windows.Forms namespace, so is typically used in window forms application only.

  • Application.ExecutablePath
    The same as Application.StartupPath, however this also includes the application name, such as “myapp.exe”

How can I get the application's path in a .NET console application?

System.Reflection.Assembly.GetExecutingAssembly().Location1

Combine that with System.IO.Path.GetDirectoryName if all you want is the directory.

1As per Mr.Mindor's comment:
System.Reflection.Assembly.GetExecutingAssembly().Location returns where the executing assembly is currently located, which may or may not be where the assembly is located when not executing. In the case of shadow copying assemblies, you will get a path in a temp directory. System.Reflection.Assembly.GetExecutingAssembly().CodeBase will return the 'permanent' path of the assembly.

How to get the path of the directory where the .exe file for a .Net Core console application is located?

I have found the answer thanks to this issue: https://github.com/dotnet/runtime/issues/13051

You have to use: Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName)

How to get actual path to executable when using .netcore 3.0 and using the /p:PublishSingleFile=true flag?

The following seems to give the path you're after, to the original executable:

System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

Get current folder path

You should not use Directory.GetCurrentDirectory() in your case, as the current directory may differ from the execution folder, especially when you execute the program through a shortcut.

It's better to use Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); for your purpose. This returns the pathname where the currently executing assembly resides.

While my suggested approach allows you to differentiate between the executing assembly, the entry assembly or any other loaded assembly, as Soner Gönül said in his answer,

System.IO.Path.GetDirectoryName(Application.ExecutablePath);

may also be sufficient. This would be equal to

System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);


Related Topics



Leave a reply



Submit