Get Current .NET CLR version at runtime?

前端 未结 4 2094
南旧
南旧 2020-12-10 10:13

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

相关标签:
4条回答
  • 2020-12-10 10:35

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

    0 讨论(0)
  • Check out the System.Environment.Version property.
    https://docs.microsoft.com/en-us/dotnet/api/system.environment.version

    0 讨论(0)
  • 2020-12-10 10:45

    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 讨论(0)
  • 2020-12-10 11:02

    Since .NET 4.5 you can't really use System.Environment.Version (it will 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).

    Instead (as @jim-w mentioned) you have to check the registry against a "simple" lookup table. It's a bit ridiculous and being Windows-specific, does not work for .NET Core ...

    However ... starting in .NET 4.7.1, they've back-ported a class from .NET Core into the full framework, and you can now check System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription

    Unfortunately, it returns a string with either: ".NET Core", ".NET Framework", or ".NET Native" before the version number -- so you still have some parsing to do.

    0 讨论(0)
提交回复
热议问题