Get Current .NET CLR version at runtime?

Viewed 20699

How can I get the current CLR Runtime version in a running .NET program ?

5 Answers

Since .NET 4.5 you could just use System.Environment.Version (it would only return 4.0.{something}, allowing you to verify that you're "at least" on 4.0 but not telling you which actual version is available unless you can map a full list of build numbers in).

In .NET Core, and starting in .NET 4.7.1 of Framework, you can check System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription

It returns a string with either: ".NET Core", ".NET Framework", or ".NET Native" before the version number -- so you have a little parsing to do if you just want the number.

That works for me:

public static String GetRunningFrameworkVersion()
{
    String netVer = Environment.Version;
    Assembly assObj = typeof( Object ).GetTypeInfo().Assembly;
    if ( assObj != null )
    {
        AssemblyFileVersionAttribute attr;
        attr = (AssemblyFileVersionAttribute)assObj.GetCustomAttribute( typeof(AssemblyFileVersionAttribute) );
        if ( attr != null )
        {
            netVer = attr.Version;
        }
    }
    return netVer;
}

I compiled my .NET program for .NET 4.5 and it returns for running under .NET 4.8:

"4.8.4150.0"

If you just want to find out the version and don't need it to be parsed or of type Version, just do this:

Console.WriteLine(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);

If your app is running via .Net Framework 4.7.2, it returns something like .NET Framework 4.7.3875.0.

If your app is running via .Net 6, it returns something like .NET 6.0.0-rtm.21522.10.

Related