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
       
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章