欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# detect latest .net framework installed on PC

程序员文章站 2022-06-14 19:09:34
static void GetNetVersionDemo() { using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(@... ......

 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!";
        }