2008-08-25 82 views
4

Compact Framework不支持Assembly.GetEntryAssembly來確定啓動.exe。那麼是否有另一種方法來獲取正在執行的.exe的名稱?如何獲得執行.exe的名稱?

編輯:我發現彼得腳的博客答案:http://peterfoot.net/default.aspx 以下是代碼:

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

in CF2:string s = System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly()。GetName()。CodeBase); – josef 2012-12-01 18:35:19

回答

4

我不知道它是否從託管代碼的工作(或者甚至緊湊架構),但在Win32中您可以調用GetModuleFileName來查找正在運行的exe文件。

MSDN: GetModuleFileName

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

但如果你把它放在一個DLL組件,我相信它會給你的程序集文件名稱。

「Full」框架上的相同調用將返回帶有「file:\」前綴的.exe文件。