使用C#檢驗.NET FrameWork版本

weixin_34162695發表於2018-03-30

程式碼如下:

public static bool checkFrameWork(string destVersion)
        {
            bool ver1 = GetVersionFromRegistry(destVersion);
            if (ver1)
                return true;
            else
            {
                return Get45PlusFromRegistry();
            }
        }
        //reference:https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
        public static bool 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)
                {
                    return CheckFor45PlusVersion((int)ndpKey.GetValue("Release"));
                }
                return false;
            }
        }

        // Checking the version using >= will enable forward compatibility.
        public static bool CheckFor45PlusVersion(int releaseKey)
        {
            if (releaseKey >= 378389)
                return true;
            // This code should never execute. A non-null release key should mean
            // that 4.5 or later is installed.
            return false;
        }
        public static bool GetVersionFromRegistry(string destVersion)
        {
            ArrayList versions = new ArrayList();
            // Opens the registry key for the .NET Framework entry.
            using (RegistryKey ndpKey =
                RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
                OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            {
                // As an alternative, if you know the computers you will query are running .NET Framework 4.5 
                // or later, you can use:
                // using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, 
                // RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
                foreach (string versionKeyName in ndpKey.GetSubKeyNames())
                {
                    if (versionKeyName.StartsWith("v"))
                    {
                        RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
                        string name = (string)versionKey.GetValue("Version", "");
                        string sp = versionKey.GetValue("SP", "").ToString();
                        string install = versionKey.GetValue("Install", "").ToString();
                        if (install == "") //no install info, must be later.
                        {
                            Console.WriteLine(name);
                            versions.Add(name);
                        }
                        //Console.WriteLine(versionKeyName + "  " + name);
                        else
                        {
                            if (sp != "" && install == "1")
                            {
                                //Console.WriteLine(versionKeyName + "  " + name + "  SP" + sp);
                                Console.WriteLine(name);
                                versions.Add(name);
                            }

                        }
                        if (name != "")
                        {
                            continue;
                        }
                        foreach (string subKeyName in versionKey.GetSubKeyNames())
                        {
                            RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
                            name = (string)subKey.GetValue("Version", "");
                            if (name != "")
                                sp = subKey.GetValue("SP", "").ToString();
                            install = subKey.GetValue("Install", "").ToString();
                            if (install == "") //no install info, must be later.
                            {
                                //Console.WriteLine(versionKeyName + "  " + name);
                                //Console.WriteLine(name);
                                versions.Add(name);
                            }
                            else
                            {
                                if (sp != "" && install == "1")
                                {
                                    //Console.WriteLine("  " + subKeyName + "  " + name + "  SP" + sp);
                                    //Console.Write(name);
                                    versions.Add(name);
                                }
                                else if (install == "1")
                                {
                                    //Console.WriteLine("  " + subKeyName + "  " + name);
                                    //Console.WriteLine(name);
                                    versions.Add(name);
                                }
                            }
                        }
                    }
                }
                object[] verArr = versions.ToArray();
                foreach (object o in verArr)
                {
                    Version ver;
                    string str = o.ToString();
                    if (str == "")
                        ver = new Version("1.0");
                    else
                        ver = new Version(str);
                    if (ver >= new Version(destVersion))
                        return true;
                }
            }
            return false;
        }
複製程式碼

其中還有一點小瑕疵,有興趣的自己看一看修改一下就好了。

在使用的時候,我們只需要呼叫checkFrameWork函式就可以了。

相關文章