50

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

5 Answers 5

52

Check out the System.Environment.Version property.
https://learn.microsoft.com/en-us/dotnet/api/system.environment.version

3
  • 1
    .NET 4.5 will return a version number starting with 4.0. See en.wikipedia.org/wiki/List_of_.NET_Framework_versions
    – Brian Low
    Aug 12, 2014 at 0:59
  • 5
    Warning from learn.microsoft.com/en-us/dotnet/framework/migration-guide/… "For the .NET Framework 4.5 and later, we do not recommend using the System.Environment.Version property to detect the version of the runtime. Instead, we recommend that you query the registry, as described in the To find .NET Framework versions by querying the registry in code (.NET Framework 4.5 and later) section earlier in this article." blargh.
    – Jim W
    Sep 12, 2017 at 17:53
  • 2
    This answer may perhaps been valid back in 2009, but it is totally wrong today. System.Environment.Version will return 4.0.30319.42000 for any DotNet version later than 4.5.1.
    – Mike Nakis
    Aug 5, 2021 at 11:21
26

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.

4
  • Later versions include more info -- .NET Framework 4.7.3190.0
    – jmoreno
    Dec 27, 2018 at 17:23
  • The registry checking is not a good solution because it tells you which Framework versions are installed, not which one the app is running on. You could have .Net Framework 4.0, 4.1, 4.2, ...., 4.8 installed and the app could run on any of them or maybe even via the .Net5 runtime...
    – hardfork
    Apr 4, 2022 at 7:11
  • They should use the FrameworkDescription class I mentioned, @ndsvw
    – Jaykul
    May 19, 2022 at 2:58
  • 1
    I'm taking the registry checking information out, because there's really so few reasons to bother with it.
    – Jaykul
    May 19, 2022 at 3:06
8

Try Environment.Version to get that info. Also you may need to call ToString().

0
5

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"

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.