【C#】获取已安装的NETFramework版本集合

代码

    /// 
    /// Windows信息
    /// 
    public partial class WindowsInfo
    {
        /// 
        /// 获取已安装的NETFramework版本集合
        /// 
        /// 
        public static List GetInstalledNETFrameworkVersions()
        {
            List installedNETFrameworkVersions = new List();

            RegistryKey ndpKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, Dns.GetHostName()).OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\");
            foreach (string versionKeyName in ndpKey.GetSubKeyNames())
            {
                if (versionKeyName.StartsWith("v", false, System.Globalization.CultureInfo.CurrentCulture))
                {
                    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.
                    {
                        installedNETFrameworkVersions.Add(versionKeyName + " " + name);
                    }
                    else
                    {
                        if (sp != "" && install == "1")
                        {
                            installedNETFrameworkVersions.Add(versionKeyName + " " + name + " SP" + sp);
                        }
                    }
                    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.
                        {
                            installedNETFrameworkVersions.Add(versionKeyName + " " + name);
                        }
                        else
                        {
                            if (sp != "" && install == "1")
                            {
                                installedNETFrameworkVersions.Add(subKeyName + " " + name + " SP" + sp);
                            }
                            else if (install == "1")
                            {
                                installedNETFrameworkVersions.Add(subKeyName + " " + name);
                            }
                        }
                    }
                }
            }

            return installedNETFrameworkVersions;
        }

    }

可以获取下面的信息

Installed .NET Framework:
v1.1.4322  SP0
1033  SP0
v2.0.50727 2.0.50727.4927 SP2
v3.0 3.0.30729.4926 SP2
v3.5 3.5.30729.4926 SP1
v4 
Client 4.8.03752
Full 4.8.03752
v4.0 
Client 4.0.0.0

你可能感兴趣的:(C#,C#)