测试篇 c#遍历所有安装程序 获取所有已经安装的程序

  /// <summary>
        /// 获取所有已经安装的程序
        /// </summary>
        /// <param name="reg"></param>
        /// <returns>程序名称,安装路径</returns> 
        private static List<Dictionary<string, string>> GetProgramAndPath()
        {
            var reg = new string[] {
                @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
                @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
            };
            string tempType = null;
            int softNum = 0;//所有已经安装的程序数量
            RegistryKey currentKey = null;
            var ls = new List<Dictionary<string, string>>();

            foreach (var item222 in reg)
            {
                object displayName = null, uninstallString = null, installLocation = null, releaseType = null;
                RegistryKey pregkey = Registry.LocalMachine.OpenSubKey(item222);//获取指定路径下的键 
                foreach (string item in pregkey.GetSubKeyNames())               //循环所有子键
                {
                    currentKey = pregkey.OpenSubKey(item);
                    displayName = currentKey.GetValue("DisplayName");           //获取显示名称
                    installLocation = currentKey.GetValue("InstallLocation");   //获取安装路径
                    uninstallString = currentKey.GetValue("UninstallString");   //获取卸载字符串路径
                    releaseType = currentKey.GetValue("ReleaseType");           //发行类型,值是Security Update为安全更新,Update为更新
                    bool isSecurityUpdate = false;
                    if (releaseType != null)
                    {
                        tempType = releaseType.ToString();
                        if (tempType == "Security Update" || tempType == "Update")
                        {
                            isSecurityUpdate = true;
                        }
                    }
                    if (!isSecurityUpdate && displayName != null && uninstallString != null)
                    {
                        softNum++;
                        if (installLocation == null)
                        {
                            ls.Add(new Dictionary<string, string> { { displayName.ToString(), "" } });
                        }
                        else
                        {
                            ls.Add(new Dictionary<string, string> { { displayName.ToString(), installLocation.ToString() } });
                        }
                    }
                }
            }
            return ls;
        }
View Code

 

posted @ 2019-02-26 19:18  惊惊  阅读(2357)  评论(0编辑  收藏  举报