VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > 简明python教程 >
  • C# detect latest .net framework installed on PC

复制代码
 static void GetNetVersionDemo()
        {
            using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            {
                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 = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full";
            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!";
        }
复制代码

相关教程