如何从exe中找出目标框架名称和版本? [英] How to find out Target framework name and version from exe?

查看:19
本文介绍了如何从exe中找出目标框架名称和版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some exe files which has been created using either .net framework 4.5 or .net core 2.1 or .net core 3.1.

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

I want to get framework name and version information from this DLL using only c# application.

我写了下面的一段代码,这些代码对 DLL 文件很有用,但对 exe 无效.

I have written below piece of code which is beneficial and works great with DLL files but not with 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 文件有用:

I have also gone through these links but I didn't found them useful for exe files:

来自exe文件的信息

确定 .NET Framework 的 dll 版本

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

Please let me know if any suggestions available.

推荐答案

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

The following program should display the version of the assembly. The program loads two assemblies during runtime using Assembly.LoadFrom method. 1) is a .NET Fx assembly and 2) is a .NET Core assembly. It loads both and displays the framework version without issues. This project is in github. If you are using the github project, you need to have .NET Core 3.1 installled.

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

这篇关于如何从exe中找出目标框架名称和版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆