【问题标题】:How could we found one exe file is .Net exe or regular exe? [duplicate]我们怎么会发现一个 exe 文件是 .Net exe 或常规 exe? [复制]
【发布时间】:2012-02-22 14:05:23
【问题描述】:

可能重复:
How do I tell if a win32 application uses the .NET runtime

我有一个文件名,我检查了这个文件是否存在于当前目录中,我的问题是我想知道这个文件(文件是可执行文件)是.Net exe还是普通exe。

【问题讨论】:

  • 这是一个编程问题,还是您只是在问如何找出答案?
  • 困难,因为操作系统不知道。 .exe 包含启动 CLR 并将程序集传递给 CLR 执行的代码。您可以尝试查找该代码的某些签名并在文件中查找它,或者在文件中搜索与元数据相关的签名。
  • 您要搜索 EXE 文件吗?
  • 在我的问题中,取决于这些 exe 的类型,我必须做一些常规操作

标签: .net file exe


【解决方案1】:

您可以使用Assembly.LoadFrom 方法。如果程序集不是 .NET 程序集,或者依赖于较新版本的框架,这将引发异常。只要您使用的是最新版本的框架,以下示例只会在路径不是 .NET 程序集时返回 false。

public bool IsNetAssembly (string path)
{
    try
    {
         Assembly assembly = Assembly.LoadFrom(path);
         return true;
    }
    catch (BadImageFormatException e)
    {
         Console.WriteLine("This is either not a .NET assembly, or is " + 
                           "later than the current .NET framework");
         return false;
    }
}

【讨论】:

    【解决方案2】:

    构造一个AssemblyName,并在try-catch 块中调用Assembly.Load。如果您没有收到异常,则它是一个 .NET 程序集。这不是万无一失的,但可以提供一种合理的入门方式。

    【讨论】:

    • 仅供参考。但是如果我使用 32 位 prog 并在 64 位程序集上尝试这种方法会发生什么?
    • @Steve BIFE,当然。
    【解决方案3】:

    你可以这样做:

    try
    {
        MessageBox.Show(System.Reflection.Assembly.LoadFrom(@"C:\test\csharptestapp.exe").ImageRuntimeVersion);         //Valid, will return 4.0
        MessageBox.Show(System.Reflection.Assembly.LoadFrom(@"C:\Windows\notepad.exe").ImageRuntimeVersion);            //Invalid, will throw BadImageFormatException
    }
    catch (BadImageFormatException ex)
    {
        MessageBox.Show(ex.ToString());
    }
    

    【讨论】:

      【解决方案4】:

      这里有一个类似的问题: How do I tell if a win32 application uses the .NET runtime

      上面写着要使用微软的PEVerify Tool

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-14
        • 1970-01-01
        • 2011-04-17
        • 1970-01-01
        相关资源
        最近更新 更多