How to Get the Name of the Current Executable in C#

How do I get the name of the current executable in C#?

System.AppDomain.CurrentDomain.FriendlyName

How do I get the name of the current executable in C#? (.NET 5 edition)

After watching everything fail, it became necessary to P/Invoke stuff to make this work. While Process.GetCurrentProcess().MainModule?.FileName reliably returns the executable binary (at least when not running under the debugger), this does not provide the command invocation the binary was launched with.

On Windows, GetCommandLine() is P/Invokable and needs only some parsing to get the information. On *n?x, reading /proc/self/cmdline does the same job.

I built a library encapsulating this. https://github.com/joshudson/Emet/tree/master/MultiCall You can find binaries on nuget.org ready to go.

I should have self-answered a long time ago. Better late than never. Nobody seemed to care until now.

How do I find the current executable filename?

EDIT:

As of .NET 6, the recommended approach (CA1839) is to use System.Environment.ProcessPath


Original answer:

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

Getting name of the current executable from assembly vs process?

ProcessName is the name of the Operating System host process.

Assembly CodeBase points to an assembly inside a given process. The same assembly can be hosted by different processes.

How to get exe application name and version in C# Compact Framework

Getting the app name:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

Getting the version:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

You might want to use GetCallingAssembly() or getting the assembly by type (e.g. typeof(Program).Assembly) if your DLL is trying to get the EXE version and you don't have immediate access to it.

EDIT

If you have a DLL and you need the name of the executable you have a few options, depending on the use case. You can get the Assembly from a type contained in the EXE assembly, but since it would be rare for the DLL to reference the EXE, it requires the EXE pass in an object of that type.

Version GetAssemblyVersionFromObjectType(object o)
{
o.GetType().Assembly.GetName().Version;
}

You could also do a little bit of an end-run like this:

[DllImport("coredll.dll", SetLastError = true)]
private static extern int GetModuleFileName(IntPtr hModule, StringBuilder lpFilename, int nSize);

...

var name = new StringBuilder(1024);
GetModuleFileName(IntPtr.Zero, name, 1024);
var version = Assembly.LoadFrom(name.ToString()).GetName().Version;

c# How to get the version of This current executable .exe

GetExecutingAssembly() returns the assembly that contains the method thaτ calls it. If you call it from a library you will always get the version of the library not the application executable.

To get the application's executable use GetEntryAssembly()

Consider the following example:

In AssemblyA:

class VersionTest
{
void Test()
{
Console.Write("Executing assembly: {0}\n", Assembly.GetExecutingAssembly().GetName().ToString()); // returns AssemblyA
Console.Write("Entry assembly: {0}\n", Assembly.GetEntryAssembly().GetName().ToString()); // returns AssemblyB
}
}

In AssemblyB:

class Program
{
void Main()
{
var ver = new VersionTest();
ver.Test();
}
}

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 do I get the .exe name of a C# console application?

Full Path of your assembly:

Assembly.GetExecutingAssembly().CodeBase.Dump();

You can always extract the name with Path.GetFileName:

string codeBase = Assembly.GetExecutingAssembly().CodeBase;
string name = Path.GetFileName(codeBase);


Related Topics



Leave a reply



Submit