【问题标题】:How can I get the name of the executing .exe?如何获取正在执行的 .exe 的名称?
【发布时间】:2008-08-25 12:51:33
【问题描述】:

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

【问题讨论】:

  • 在 CF2 中:字符串 s = System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

标签: .net compact-framework windows-ce


【解决方案1】:

我不确定它是否适用于托管代码(甚至是紧凑框架),但在 Win32 中您可以调用 GetModuleFileName 来查找正在运行的 exe 文件。

MSDN: GetModuleFileName

【讨论】:

    【解决方案2】:
    string exefile = Assembly.GetExecutingAssembly().GetName().CodeBase;
    

    但是如果你把它放在一个DLL程序集中,我相信它会给你程序集文件名。

    对“Full”框架的相同调用将返回带有“file:\”前缀的 .exe 文件。

    【讨论】:

      【解决方案3】:

      在托管代码中,我认为您可以使用它: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx

      Application.ExecutablePath

      【讨论】:

      • 请注意,此解决方案仅适用于 Windows 客户端应用程序。如果是控制台或 Windows 服务,则不会有 Application 对象。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多