views:

589

answers:

3

The Compact Framework doesn't support Assembly.GetEntryAssembly to determine the launching .exe. So is there another way to get the name of the executing .exe?

EDIT: I found the answer on Peter Foot's blog: http://peterfoot.net/default.aspx Here is the code:

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);

}

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

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

I am not sure whether it works from managed code (or even the compact framework), but in Win32 you can call GetModuleFileName to find the running exe file.

MSDN: GetModuleFileName

Timbo
A: 

In managed code, i think you can use this:http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx

Application.ExecutablePath

Karim
+1  A: 

string exefile = Assembly.GetExecutingAssembly().GetName().CodeBase;

But if you put it in a DLL assembly, I belive it will give you the assembly file name. The same call on the "Full" framework will return the .exe file with a "file:\" prefix.

mliesen