不以物喜,不以己悲

C#获取已安装程序列表

查找注册表:SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall下的所有项
需要查找的位置为:HKEY_LOCAL_MACHINE和HKEY_CURRENT_USER并且要查找32位和64位的视图,而且要注意排除系统程序,可以查找SystemComponent值为1的排除掉。示例代码如下:

/// <summary>
    /// 注册表操作
    /// </summary>
    public static class RegHelper
    {
        #region 属性
        public const string UninstallKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
        #endregion

        #region 私有变量
        #endregion

        #region 构造函数
        #endregion

        #region 共有方法
        public static List<SoftModel> FindSoft()
        {
            List<SoftModel> lst = new List<SoftModel>();
            RegistryKey regUninstall = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(UninstallKey, false);
            FindSoft(regUninstall, ref lst, RegistryView.Registry32);
            regUninstall = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(UninstallKey, false);
            FindSoft(regUninstall, ref lst, RegistryView.Registry64);
            regUninstall = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry32).OpenSubKey(UninstallKey, false);
            
            FindSoft(regUninstall, ref lst, RegistryView.Registry32);
            regUninstall = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64).OpenSubKey(UninstallKey, false);
            FindSoft(regUninstall, ref lst, RegistryView.Registry64);
            return lst;
        }
        #endregion

        #region 私有方法
        private static void FindSoft(RegistryKey regUninstall, ref List<SoftModel> lst, RegistryView registryView)
        {
            foreach (var item in regUninstall.GetSubKeyNames())
            {
                RegistryKey regSub = regUninstall.OpenSubKey(item, false);
                string displayName = regSub.GetValue("DisplayName") as string;
                string installLocation = regSub.GetValue("InstallLocation") as string;
                string uninstallString = regSub.GetValue("UninstallString") as string;
                string displayVersion = regSub.GetValue("DisplayVersion") as string;
                string installDate = regSub.GetValue("InstallDate") as string;
                string publisher = regSub.GetValue("Publisher") as string;
                string displayIcon = regSub.GetValue("DisplayIcon") as string;
                int estimatedSize = (int)regSub.GetValue("EstimatedSize", 0);
                int systemComponent = (int)regSub.GetValue("SystemComponent", 0);
                do
                {
                    
                    if (string.IsNullOrWhiteSpace(displayName))
                    {
                        break;
                    }
                    if (string.IsNullOrWhiteSpace(uninstallString))
                    {
                        break;
                    }
                    if (string.IsNullOrWhiteSpace(displayVersion) && string.IsNullOrWhiteSpace(displayIcon))
                    {
                        break;
                    }
                    if(systemComponent == 1) // 排除系统应用
                    {
                        break;
                    }
                    if (string.IsNullOrWhiteSpace(installDate) && !string.IsNullOrWhiteSpace(displayIcon))
                    {
                        try
                        {
                            string[] array = displayIcon.Split(',');
                            if(array.Length >= 2)
                            {
                                uninstallString = array[0];
                            }
                            FileInfo fileInfo = new FileInfo(uninstallString);
                            installDate = fileInfo.CreationTime.ToShortDateString(); //使用文件创建时间作为安装时间
                        }
                        catch(Exception ex)
                        {

                        }
                    }
                    SoftModel softModel = lst.FirstOrDefault(item1 => item1.Name == displayName);

                    if(softModel == null)
                    {
                        SoftModel model = new SoftModel
                        {
                            Name = displayName,
                            Version = displayVersion,
                            DateTime = installDate,
                            UninstallString = uninstallString,
                            Publisher = publisher,
                            EstimatedSize = estimatedSize == 0 ? "未知" : (estimatedSize / 1024.0).ToString("0.00M"),
                            RegKey = regSub.ToString(),
                            InstallLocation = installLocation,
                            RegistryView = registryView,
                        };
                        lst.Add(model);
                    }
                    else
                    {
                        if(string.IsNullOrWhiteSpace(softModel.DateTime) && !string.IsNullOrWhiteSpace(installDate))
                        {
                            softModel.DateTime = installDate;
                        }
                    }
                } while (false);
            }
        }
       
        #endregion
       
    }
posted @ 2022-01-13 14:07  这种人  阅读(1342)  评论(0编辑  收藏  举报