开发者

Identify my dotnet version [duplicate]

This question already has answers here: Is there an easy way to check the .NET Framework version? (23 answers) Closed 2 years ago.

How can I check fro开发者_如何学编程m a c# application itself, what version of dotnet being used by application?


Use Environment.Version - it gives you the exact version of .NET running the application.

Gets a Version object that describes the major, minor, build, and revision numbers of the common language runtime.


To find out what version of the framework is installed, see this SO question and answers. In a nut shell, you will need to dig into the registry.


You can use the:

Environment.Version

to get the version number of the .NET runtime.


Create a Console app add this class and run it

 using Microsoft.Win32;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication2
    {
        public class GetDotNetVersion
        {
            public static void Get45PlusFromRegistry()
    {
        const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
        using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
        {
            if (ndpKey != null && ndpKey.GetValue("Release") != null)
            {
                Console.WriteLine(".NET Framework Version: " +  CheckFor45PlusVersion((int)ndpKey.GetValue("Release")));
            }
            else
            {
                Console.WriteLine(".NET Framework Version 4.5 or later is not detected.");
            }
        }
    }

    // Checking the version using >= will enable forward compatibility.
    private static string CheckFor45PlusVersion(int releaseKey)
    {
        if (releaseKey >= 394802)
            return "4.6.2 or later";
        if (releaseKey >= 394254)
        {
            return "4.6.1";
        }
        if (releaseKey >= 393295)
        {
            return "4.6";
        }
        if ((releaseKey >= 379893))
        {
            return "4.5.2";
        }
        if ((releaseKey >= 378675))
        {
            return "4.5.1";
        }
        if ((releaseKey >= 378389))
        {
            return "4.5";
        }
        // This code should never execute. 
        // that 4.5 or later is installed.
        return "No 4.5 or later version detected";
    }
 }
 // Calling the GetDotNetVersion.Get45PlusFromRegistry method produces 
 // output like the following:
 //       .NET Framework Version: 4.6.1
 }


In your Visual Studio, go to Tools->Nutget Package Management->Package Manager Console Type in dotnet --version And here you go!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜