简体   繁体   中英

c# get framework / runtime version from application with Assembly

I'm trying to check what framework version a other .NET application is working with through a assembly. I found two ways to get the version of the framework ( first through the ImageRunetimeVersion and with the FullName of the assembly ) but i'm getting two different values from it and i dont know which is the right one:

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

if i'm going to test this with my application and of eg paint.net the results are:

Like you can see i cant say which "version" is the right one. The biggest problem is that if i'm going to take a look to my project properties for my .net application the target platform is 3.5 and not 2.0 or 1.0-

I think I can clear some things up for you. First, the FullName property gives you the application version number. That is the number you set and has no relation to the .NET framework version. That means the version number in the FullName property can be ignored.

The imageRuntimeVersion is the CLR version. Unfortunately, 2.0 covers .NET 2.0, 3.0, and 3.5. Technically, your application is giving you the right information but it isn't really the information you want (I don't think).

Here is a SO article with more explanation: Retrieve Target Framework Version and Target Framework Profile from a .Net Assembly

A couple of suggestions for you from that article include looking for a config file that would give you the targetted framework or looking at the versions of the libraries that are used. Neither is really foolproof but as far as I know, that is the best you can do.

TargetFramework not same CLR version.

For example,

CLR 4.0 TargetFramework: .NET 4.0 and .NET 4.5

A solution using TargetFrameworkAttribute http://www.lucbos.net/2011/08/get-targetframework-for-assembly.html

Note: TargetFrameworkAttribute is only available from .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);
    }

Console.WriteLine("版本:{0}", Environment.Version.ToString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM