How can I find the .NET version at runtime programmatically?

I need a solution to provide me with an executable version of .NET for both the full framework and .NET Core.

On a computer with the following versions of .NET:

Full: 4.7.2
Core: 2.1.104

Duration:

RuntimeInformation.FrameworkDescription

Gives me:

Full: .NET Framework 4.7.2558.0
Core: .NET Core 4.6.26212.01

Duration:

Environment.Version

Gives me:

Full: 4.0.30319.42000
Core: 4.0.30319.42000

How can I accurately get run-time versions on different platforms, knowing that the registry setting is not available outside of Windows.

+15
source share
2 answers

, , . , , , , , , ( , 8 ).

.NET Framework:

using System;
...
string ver = AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName;

.NET Core:

using System.Reflection;
using System.Runtime.Versioning;
...
string ver = Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName;

:

.NETFramework,Version=v4.5.1
.NETCoreApp,Version=v2.0

, , API ( Microsoft, API, ).

, , , , , . , .NET Standard 2.x .NET Standard 1.x. , - , ...

+17

, .NET Framework , " ". .NET Framework.

: https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/versions-and-dependencies

, .NET Framework. , .NET Framework 4.5 4.7.1. , . ( https://github.com/dotnet/corefx/blob/master/src/CoreFx.Private.TestUtilities/src/System/PlatformDetection.NetFx.cs#L33)

    private static Version GetFrameworkVersion()
    {
        using (RegistryKey ndpKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full"))
        {
            if (ndpKey != null)
            {
                int value = (int)(ndpKey.GetValue("Release") ?? 0);
                if (value >= 528040) 
                    return new Version(4, 8, 0);

                if (value >= 461808) 
                    return new Version(4, 7, 2);

                if (value >= 461308)
                    return new Version(4, 7, 1);

                if (value >= 460798)
                    return new Version(4, 7, 0);

                if (value >= 394802)
                    return new Version(4, 6, 2);

                if (value >= 394254)
                    return new Version(4, 6, 1);

                if (value >= 393295)
                    return new Version(4, 6, 0);

                if (value >= 379893)
                    return new Version(4, 5, 2);

                if (value >= 378675)
                    return new Version(4, 5, 1);

                if (value >= 378389)
                    return new Version(4, 5, 0);

                throw new NotSupportedException($"No 4.5 or later framework version detected, framework key value: {value}");
            }

            throw new NotSupportedException(@"No registry key found under 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' to determine running framework version");
        }
    }

, , , , .NET Framework ( , , , System.Object) ( .NET Runtime, , ).

.NET Core , . , System.Object , .

    public static Version GetVersion()
    {
        string runtimePath = System.IO.Path.GetDirectoryName(typeof(object).Assembly.Location);
        // Making the assumption that the path looks like this
        // C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.6
        string version = runtimePath.Substring(runtimePath.LastIndexOf('\\') + 1);

        return new Version(version);
    }
+5

Source: https://habr.com/ru/post/1696012/


All Articles