【问题标题】:How to find out Target framework name and version from exe?如何从 exe 中找出目标框架名称和版本?
【发布时间】:2020-05-28 07:09:55
【问题描述】:

我有一些使用 .net framework 4.5 或 .net core 2.1 或 .net core 3.1 创建的 exe 文件。

我想仅使用 c# 应用程序从此 DLL 获取框架名称和版本信息。

我在下面写了一段代码,它对 DLL 文件很有用,但对 exe 无效。

var dllInformation = Assembly.LoadFrom(@"D:\\MyProgram.dll");
Console.WriteLine(dllInformation.FullName);
Console.WriteLine(dllInformation.ImageRuntimeVersion);
Console.WriteLine(((TargetFrameworkAttribute)dllInformation.GetCustomAttributes(typeof(TargetFrameworkAttribute)).First()).FrameworkName);

我也浏览了这些链接,但我没有发现它们对 exe 文件有用:

information from exe file

Determine .NET Framework version for dll

如果有任何建议,请告诉我。

【问题讨论】:

  • Exe可​​以被其他编译器编译,与.NET/.NET Core不同
  • @mjwills 我在 Azure 中有几个可用的解决方案,我想立即知道哪些应用程序尚未在 .net 核心中迁移。我已被分配迁移所有尚未迁移到 .net core 3.1 的项目
  • @PavelAnikhouski 是的 exe 可以由另一个编译器编译,但 exe 可以使用 .net 框架构建。我想知道的只是

标签: c# .net .net-core


【解决方案1】:

以下程序应显示程序集的版本。该程序 在运行时使用Assembly.LoadFrom 方法加载两个程序集。 1) 是 .NET Fx 程序集,2) 是 .NET Core 程序集。它同时加载并显示框架版本而没有问题。该项目位于github。如果您使用的是 github 项目,则需要安装 .NET Core 3.1。

using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;

namespace net007
{
    class Program
    {
        static void Main(string[] args)
        {
            //A .net framwwork dll in the same output fodler
            //as the current executable
            var fxAssembly = Assembly.LoadFrom("fx.console.app.exe");

            //A .net core dll in the same output fodler
            //as the current executable
            var netCoreAssembly = Assembly.LoadFrom("core.console.app.dll");

            ShowFrameworkVersion(fxAssembly); //.NETFramework,Version=v4.7.2
            ShowFrameworkVersion(netCoreAssembly);//.NETCoreApp,Version = v3.1
        }

        static void ShowFrameworkVersion(Assembly assembly)
        {
            var attributes = assembly.CustomAttributes;
            foreach (var attribute in attributes)
            {
                if (attribute.AttributeType == typeof(TargetFrameworkAttribute))
                {
                    var arg = attribute.ConstructorArguments.FirstOrDefault();
                    if (arg == null)
                        throw new NullReferenceException("Unable to read framework version");
                    Console.WriteLine(arg.Value);
                }
            }
        }
    }
}

【讨论】:

  • @Soundararjan 我可以获得版本信息。你能帮我解决一下框架名称吗?
  • 框架名称的值是什么样的?
  • .Net core 2.1 或 .Net core 3.1 等
  • 它应该以以下格式显示结果,您可以使用它来查找框架的名称。 .NETFramework,Version=v4.7.2 .NETCoreApp,Version = v3.1 当你运行上述程序时,你会得到什么输出?
  • @Soundarajan 我收到无法加载其中一个 DLL 的文件或程序集的 System.Runtime 错误
【解决方案2】:

您可以使用 PowerShell 来检测目标框架版本:

$path = "C:\your dll here.dll"
[Reflection.Assembly]::ReflectionOnlyLoadFrom($path).CustomAttributes |
Where-Object {$_.AttributeType.Name -eq "TargetFrameworkAttribute" } | 
Select-Object -ExpandProperty ConstructorArguments | 
Select-Object -ExpandProperty value

【讨论】:

  • 我已经在问题中提到我只想使用 C# 应用程序来了解这一点。
  • @ManishJain 好的,如果我将脚本嵌入到您的 .NET 应用程序中并调用它会怎样?能解决你的需求吗?
  • 可能取决于执行成功
  • OP 想要一个 C# 解决方案,但在尝试对已构建的程序集进行故障排除时,PowerShell 更易于使用。另外,默认的表格输出可以截断很多值,所以我更喜欢“格式列表”:ReflectionOnlyLoadFrom($path).CustomAttributes | Where-Object {$_.AttributeType.Name -eq "TargetFrameworkAttribute" } | Format-List
【解决方案3】:

这是我获取“Name.exe”目标框架的完整课程。

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;

public class TargetVersionChecker : MarshalByRefObject
{
    public string GetTargetedFrameWork(string exePath)
    {
        Assembly fxAssembly;
        try
        {
            fxAssembly = Assembly.ReflectionOnlyLoadFrom(exePath);
            var targetFrameworkAttribute = fxAssembly.GetCustomAttributesData().FirstOrDefault(x => x.AttributeType == typeof(TargetFrameworkAttribute));
            return targetFrameworkAttribute?.ConstructorArguments.FirstOrDefault().Value.ToString();
        }
        catch (Exception ex)
        {
            // I log here the error but is to specific to our system so I removed it to be more simple code.
            return string.Empty;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2018-02-07
    • 1970-01-01
    • 2022-01-17
    • 2013-05-16
    • 2013-07-01
    • 2021-04-16
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    相关资源
    最近更新 更多