Mobile World

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.


Posted Aug 24 2008, 11:11 PM by Peter Foot
Filed under:

Comments

How to find program installation path (CodeBase unreliable) | keyongtech wrote How to find program installation path (CodeBase unreliable) | keyongtech
on 01-18-2009 12:12

Pingback from  How to find program installation path (CodeBase unreliable) | keyongtech

Copyright © 2010 APPA Mundi Limited. All Rights Reserved. Terms of Use and Privacy Policy.