How to Find the Current Executable Filename

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

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

System.AppDomain.CurrentDomain.FriendlyName

How to Get the Filename of the Currently Running Executable in C++

On windows you can use:

TCHAR szExeFileName[MAX_PATH]; 
GetModuleFileName(NULL, szExeFileName, MAX_PATH);

szExeFileName will contain full path + executable name

[edit]

For more portable solution use argv[0] or some other platform specific code. You can find such aproach here: https://github.com/mirror/boost/blob/master/libs/log/src/process_name.cpp.

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.

Get current file name after creating executable

The os package provides os.Args[0] to obtain the executable name, or if you want the whole path you could use os.Executable, as @Peter has suggested

Extracting the current executable name

First of all you want to get hold of the full path to the executable by calling GetModuleFileName passing NULL as the module handle. Then call PathFindFileName to pull out the file name component.

There is in fact a difference between GetModuleFileName and argv[0]. The latter is the name used to start the process. It could be missing the full path, but more importantly here, it could be missing the .exe extension. If you want to know the actual filename then you need to use GetModuleFileName.

C program, that prints its executable file name

You know the name of the executable when you're building it; the
simplest solution is to embed it in the program, using a -D or /D
option to define a macro on the command line.

Other than that, the generic answer is that it isn't possible:

According to the standard


  • argv[0] should contain the name which was used to invoke the
    program (whatever that means). Which is nice, but 1) it isn't even
    implementable under Unix, and 2) under most systems, there are all sorts
    of aliasing which means that the name used to invoke the program bears
    no relationship to the name of the executable.

Under Windows


  • There's a system function GetModuleFileName which can be used to
    obtain the path of the executable. Once you've got the path, the last
    element of the path is the name of your executable.

Under Unix


  • It's fundamentally impossible. When starting a new process, Unix
    takes separate arguments for the path to the executable and for what
    ends up in argv[0], so they can potentially have no relationship to
    one another. It all depends on who starts your process. bash will
    put the full path to the executable in the environment variable "_",
    so you can use getenv to get it. But that only works if your program
    was started by bash. On most Unices, you can also find it in the
    /proc filesystem, if you know your way around there; but the
    organization of this varies from one Unix to the next. Note too that
    because of hard links, you're executable may not have just one
    name.

The real question is why you want to do this. What is the problem you
are trying to solve?

Java get current file name EXE

Per the JSmooth documentation,

JSmooth also makes some special variable accessible for your application.

Form                          Meaning
${EXECUTABLEPATH} Replaced by the path to the executable binary. For
instance, if the executable binary launched is located
at c:/program files/jsmooth/test.exe, this variable
is replaced with c:/program files/jsmooth

${EXECUTABLENAME} Replaced by the name of the executable binary. For
instance, if the executable binary launched is located
at c:/program files/jsmooth/test.exe, this variable is
replaced with test.exe

You set these in JSmooth under the "Environment Settings" (the last panel), which allows you to map the variable name. So,

MY_EXECUTABLEPATH=${EXECUTABLEPATH}
MY_EXECUTABLENAME=${EXECUTABLENAME}

In your application, you can get those with

String execPath = System.getProperty("MY_EXECUTABLEPATH");
String execName = System.getProperty("MY_EXECUTABLENAME");

Find Path to any given exe file name

It seems there are many places to look to find the path to an exe. While the original code above works, it is not an exhaustive search and you also need to look in the registry key SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\ and also check the SpecialFolders Windows, System and SystemX86 (https://learn.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=netframework-4.7.2)



Related Topics



Leave a reply



Submit