c#通过程序集从应用程序获取框架/运行时版本

c#通过程序集从应用程序获取框架/运行时版本,c#,frameworks,runtime,version,C#,Frameworks,Runtime,Version,我试图通过程序集检查其他.NET应用程序使用的框架版本。我找到了两种获取框架版本的方法(首先通过ImageRunetimeVersion和程序集全名),但我从中获得了两个不同的值,我不知道哪个是正确的: Assembly ass = Assembly.LoadFrom(autPath); string imageRuntimeVersion = ass.ImageRuntimeVersion; Console.WriteLi

我试图通过程序集检查其他.NET应用程序使用的框架版本。我找到了两种获取框架版本的方法(首先通过ImageRunetimeVersion和程序集全名),但我从中获得了两个不同的值,我不知道哪个是正确的:

        Assembly ass = Assembly.LoadFrom(autPath);            
        string imageRuntimeVersion = ass.ImageRuntimeVersion;
        Console.WriteLine("ImageRunetimeVersion: " + imageRuntimeVersion);
        Console.WriteLine("FullName: " + ass.FullName);

        Console.WriteLine("");            
        Console.WriteLine("----");
        Console.WriteLine("Referenced Assemblies: ");
        Console.WriteLine(""); 

        AssemblyName[] referencedAssemblies = ass.GetReferencedAssemblies();
        foreach (AssemblyName a in referencedAssemblies)
        {
            Console.WriteLine(a.FullName);
        }
如果我要用我的应用程序和例如paint.net来测试这一点,结果是:


就像你可以看到的,我不能说哪个版本是正确的。最大的问题是,如果我要查看.net应用程序的项目属性,目标平台是3.5,而不是2.0或1.0-

我想我可以为您澄清一些事情。首先,FullName属性为您提供应用程序版本号。这是您设置的数字,与.NET framework版本无关。这意味着可以忽略FullName属性中的版本号

imageRuntimeVersion是CLR版本。不幸的是,2.0涵盖了.NET2.0、3.0和3.5。从技术上讲,您的应用程序提供了正确的信息,但它并不是您真正想要的信息(我不认为)

下面是一篇有更多解释的SO文章:


这篇文章为您提供了一些建议,包括寻找一个配置文件,为您提供目标框架,或者查看所使用的库的版本。两者都不是万无一失的,但据我所知,这是你能做的最好的了。

TargetFramework不是同一个CLR版本

比如说,

CLR 4.0 TargetFramework:.NET 4.0和.NET 4.5

使用TargetFrameworkAttribute的解决方案

注意:TargetFrameworkAttribute仅在.NET 4.0中可用

    var targetFramework = "Unknown";
    var targetFrameworkAttributes = assembly.GetCustomAttributes(typeof(System.Runtime.Versioning.TargetFrameworkAttribute), true);
    if (targetFrameworkAttributes.Length > 0)
    {
        var targetFrameworkAttribute = (TargetFrameworkAttribute)targetFrameworkAttributes.First();
        targetFramework = (targetFrameworkAttribute.FrameworkDisplayName);
    }

“ImageRunetimeVersion”中的版本与通过全名调用的版本不同。最后一张图片:第一个版本:v.2.0.50727第二个版本:1.0.0.0。现在我不知道这两个版本中哪一个是应用程序使用的正确框架版本。我怀疑我从显示的版本号中得到了错误的想法,感谢您的许可。