如何获取正在执行的 .exe 的名称?
-
09-06-2019 - |
题
Compact Framework 不支持 Assembly.GetEntryAssembly 来确定启动 .exe。那么还有其他方法来获取正在执行的 .exe 的名称吗?
编辑:我在 Peter Foot 的博客上找到了答案: 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);
解决方案
我不确定它是否适用于托管代码(甚至紧凑框架),但在 Win32 中,您可以调用 GetModuleFileName 来查找正在运行的 exe 文件。
其他提示
string exefile = Assembly.GetExecutingAssembly().GetName().CodeBase;
但如果你把它放在一个DLL程序集中,我相信它会给你程序集文件名。
对“完整”框架的相同调用将返回带有“file:\”前缀的 .exe 文件。
在托管代码中,我认为你可以使用这个:http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx
应用程序.可执行路径
不隶属于 StackOverflow