zoukankan      html  css  js  c++  java
  • C# detect latest .net framework installed on PC

    
    
     static void GetNetVersionDemo()
            {
                using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@"SOFTWAREMicrosoftNET Framework SetupNDP"))
                {
                    foreach(var versionKeyName in ndpKey.GetSubKeyNames())
                    {
                        //Skip .NET Framework 4.5 version information.
                        if(versionKeyName=="v4")
                        {
                            continue;
                        }
    
                        if(versionKeyName.StartsWith("v"))
                        {
                            RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
    
                            //Get the .NET Framework version value.
                            var name = (string)versionKey.GetValue("Version", "");
    
                            //Get the service pack number.
                            var sp = versionKey.GetValue("SP", "").ToString();
    
                            //Get the installation flag,or an empty string if there is none.
                            var install = versionKey.GetValue("Install", "").ToString();
    
                            if(string.IsNullOrEmpty(install))
                            {
                                Console.WriteLine($"{versionKeyName} {name}");
                            }
                            else
                            {
                                if(!string.IsNullOrEmpty(sp) && install=="1")
                                {
                                    Console.WriteLine($"{versionKeyName} {name} SP{sp}");
                                }
                            }
    
                            if(!string.IsNullOrEmpty(name))
                            {
                                continue;
                            }
    
                            foreach(var subKeyName in versionKey.GetSubKeyNames())
                            {
                                RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                                name = (string)subKey.GetValue("Version", "");
                                if(!string.IsNullOrEmpty(name))
                                {
                                    sp = subKey.GetValue("SP", "").ToString();
                                }
    
                                install = subKey.GetValue("Install", "").ToString();
                                if(string.IsNullOrEmpty(install))
                                {
                                    Console.WriteLine($"{versionKeyName} {name}");
                                }
                                else
                                {
                                    if((!string.IsNullOrEmpty(sp)) && install=="1")
                                    {
                                        Console.WriteLine($"{subKeyName} {name}  SP{sp}");
                                    }
                                    else
                                        if(install=="1")
                                    {
                                        Console.WriteLine($"{subKeyName} {name}");
                                    }
                                }
                            }
                        }
                    }
                }
            }
    
    
    
    using Microsoft.Win32;
    
    static void Main(string[] args)
            {
                GetDotNetFrameworkVersion();
                Console.ReadLine();
            }
    
            static void GetDotNetFrameworkVersion()
            {
                const string subKey = @"SOFTWAREMicrosoftNET Framework SetupNDPv4Full";
                using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subKey))
                {
                    if(ndpKey!=null && ndpKey.GetValue("Release")!=null)
                    {
                        var objResult = ndpKey.GetValue("Release");
                        var versionResult = CheckFor45PlusVersion((int)objResult);
                        Console.WriteLine($".NET Framework Version:{versionResult}");
                    }
                    else
                    {
                        Console.WriteLine(".NET Framework Version 4.5 or later is not detected!");
                    }
                }
            }
    
            //Convert the Main.Minor.Build.Revision
            static string CheckFor45PlusVersion(int releaseKey)
            {             
                if(releaseKey>=528040)
                {
                   return "4.8 or later";
                }
    
                if(releaseKey>=461808)
                {
                    return "4.7.2";
                }
    
                if(releaseKey>=461308)
                {
                    return "4.7.1";
                }
    
                if (releaseKey >= 460798)
                {
                    return "4.7";
                }
    
                if(releaseKey>=394802)
                {
                    return "4.6.2";
                }
    
                if(releaseKey>=394254)
                {
                    return "4.6.1";
                }
    
                if(releaseKey>=393295)
                {
                    return "4.6";
                }
    
                if(releaseKey>=393295)
                {
                    return "4.5.2";
                }
    
                if(releaseKey>=378675)
                {
                    return "4.5.1";
                }
    
                if(releaseKey>=378389)
                {
                    return "4.5";
                }
    
                return "No 4.5 or later version detected!";
            }
  • 相关阅读:
    java设计模式--桥接模式
    java设计模式--单例模式
    java设计模式--迭代器模式
    java设计模式--组合模式
    java设计模式--备忘录模式
    java设计模式--适配器模式
    洛谷P1464 Function
    洛谷P2434 [SDOI2005]区间
    p1416攻击火星
    p1359租用游艇
  • 原文地址:https://www.cnblogs.com/Fred1987/p/11954327.html
Copyright © 2011-2022 走看看