Exemplo n.º 1
0
        /// <summary>
        /// 获取.net 4.0或以上的版本信息
        /// </summary>
        /// <param name="versionKey"></param>
        /// <returns></returns>
        private static DotNetFrameworkInfo GetV4NetFrameworkInfo(RegistryKey versionKey)
        {
            DotNetFrameworkInfo info = null;
            var install = versionKey.GetValue("Install", "").ToString();

            if (!string.IsNullOrEmpty(install))
            {
                var keyName     = versionKey.Name;
                var name        = $"v{versionKey.GetValue("Version", "")} {keyName.Substring(keyName.LastIndexOf('\\') + 1)}";
                var installPath = versionKey.GetValue("InstallPath", "").ToString();
                var sp          = versionKey.GetValue("SP", "").ToString();
                info = new DotNetFrameworkInfo(name, name, installPath, sp);
            }
            return(info);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取已安装的.NET Framework版本信息
        /// </summary>
        /// <returns></returns>
        public static DotNetFrameworkInfo[] GetAllNetFrameworkInfo()
        {
            RegistryKey ndpKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
                                 OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\");

            List <DotNetFrameworkInfo> infoList = new List <DotNetFrameworkInfo>();

            foreach (string versionKeyName in ndpKey.GetSubKeyNames())
            {
                if (versionKeyName.StartsWith("v"))
                {
                    RegistryKey versionKey  = ndpKey.OpenSubKey(versionKeyName);
                    var         install     = versionKey.GetValue("Install", "").ToString();
                    var         version     = versionKey.GetValue("Version", "").ToString();
                    var         installPath = versionKey.GetValue("InstallPath", "").ToString();
                    var         sp          = versionKey.GetValue("SP", "").ToString();
                    if (!string.IsNullOrEmpty(install))
                    {
                        infoList.Add(new DotNetFrameworkInfo(versionKeyName, version, installPath, sp));
                    }
                    else
                    {
                        foreach (string subKeyName in versionKey.GetSubKeyNames())
                        {
                            RegistryKey         subVersionKey = versionKey.OpenSubKey(subKeyName);
                            DotNetFrameworkInfo info          = GetV4NetFrameworkInfo(subVersionKey);
                            if (info != null)
                            {
                                infoList.Add(info);
                            }
                        }
                    }
                }
            }

            ndpKey.Close();
            return(infoList.ToArray());
        }