Get the name of your executing .exe

The Compact Framework doesn’t support Assembly.GetEntryAssembly to determine the launching .exe. You can instead P/Invoke the native GetModuleFileName function like so:-

byte[] buffer = new byte[MAX_PATH * 2];

int chars = GetModuleFileName(IntPtr.Zero, buffer, MAX_PATH);

if (chars > 0)


string assemblyPath = System.Text.Encoding.Unicode.GetString(buffer, 0, chars * 2);


Where MAX_PATH is defined in the Windows CE headers as 260. The P/Invoke declaration for GetModuleFileName looks like this:-

[DllImport(“coredll.dll”, SetLastError = true)]

private static extern int GetModuleFileName(IntPtr hModule, byte[] lpFilename, int nSize);

The function expects a HMODULE – a handle to a native module. However passing IntPtr.Zero here indicates we want the module which created the process which is our .exe. This code will always return the path of the calling .exe regardless of if it is in a utility dll, or even a GAC assembly located in the Windows folder.

2 thoughts on “Get the name of your executing .exe”

  1. That works only if the code is within the original .exe itself. If you want to re-use the code from a dll it will return the path to the dll not the application which may be in a different folder (e.g. in the GAC). So this is a sure-fire way to get the calling .exe. In the full .NET framework you would use Assembly.GetEntryAssembly()


Comments are closed.