Determine framework (CLR) version of assembly

41,255

Solution 1

ildasm.exe will show it if you double-click on "MANIFEST" and look for "Metadata version". By default, it's the version that the image was compiled against.

Solution 2

One clarification...

The problem with all the mentioned methods is that they will return version 4.0 if assembly was compiled against .NET framework 4.0, 4.5 or 4.5.1.

The way to figure out this version programmatically at runtime is using the System.Runtime.Versioning.TargetFrameworkAttribute for the given assembly, for example

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

...    

object[] list = Assembly.GetExecutingAssembly().GetCustomAttributes(true);
var attribute = list.OfType<TargetFrameworkAttribute>().First();

Console.WriteLine(attribute.FrameworkName);
Console.WriteLine(attribute.FrameworkDisplayName);

Will return

a.FrameworkName ".NETFramework,Version=v4.0"    string
a.FrameworkDisplayName  ".NET Framework 4"      string

a.FrameworkName ".NETFramework,Version=v4.5"    string
a.FrameworkDisplayName  ".NET Framework 4.5"    string

a.FrameworkName ".NETFramework,Version=v4.5.1"  string
a.FrameworkDisplayName  ".NET Framework 4.5.1"  string

Solution 3

class Program {
  static void Main(string[] args) { 
      System.Console.WriteLine(
             System.Reflection.Assembly.LoadFrom(args[0]).ImageRuntimeVersion);
  }
}

Compile and run the above application under the latest .NET Framework (as an older CLR may be unable to load assemblies requiring a newer CLR) and run it passing the path to the assembly you want to check as the command line argument.

Solution 4

Here's a PowerShell equivalent of the .NET code suggested in another answer. Using PowerShell means that you can skip a few steps like creating and compiling an assembly.

At a PowerShell prompt, run the following:

[System.Reflection.Assembly]::LoadFrom("C:\...\MyAssembly.dll").ImageRuntimeVersion

By default, PowerShell uses the .NET v2 runtime, so you'll get an exception for assemblies targetting v4. Stack Overflow question How can I run PowerShell with the .NET 4 runtime? details methods for changing that, if required.

Solution 5

Here is a powershell one liner that will display the Target framework version for assemblies targeting v4 and up.

 Resolve-Path($args) | Select @{N='Assembly'; E={$_ }}, @{N='TargetFramework'; E={(([Reflection.Assembly]::ReflectionOnlyLoadFrom($_).GetCustomAttributesData() | Where-Object { $_.AttributeType -like "System.Runtime.Versioning.TargetFrameworkAttribute" })).NamedArguments.TypedValue}} | Format-Table

use:

C:\test\> show-targetfw.ps1 *.dll

Assembly             TargetFramework
--------             --------
C:\test\a.dll        ".NET Framework 4.6.1"
C:\test\b.dll        ".NET Framework 4.5.2"
Share:
41,255
Klaus Byskov Pedersen
Author by

Klaus Byskov Pedersen

Thanks for visiting! I have been coding for more than 20 years, most of the time as an independent consultant. I'm currently working on an automated trading system. Danish visitors may want to grab a few bottles of wine from my wineshop copenhagenwine.dk.

Updated on July 08, 2022

Comments

  • Klaus Byskov Pedersen
    Klaus Byskov Pedersen almost 2 years

    From the command line (or by any means really), how can I determine which CLR version a .NET assembly requires?

    I need to determine if an assembly requires 2.0 or 4.0 CLR version.