通过注册表的方式监测某个应用是否安装或者卸载

发布时间 2023-05-29 11:31:49作者: log9527

 找了很多例子,要么就是不存在事件,要么就是报错,要么就是事件不触发。所以使用了最原始的办法 定时器+注册表的变化

以下是监测wps或者office安装的例子:

private System.Timers.Timer _monitorTimer;/// <summary>
        /// 卸载信息列表
        /// </summary>
        private List<UninstallInfoData> _uninstallInfos;
        protected override async Task ExecuteAsync()
        {
            try
            {
                _uninstallInfos = GetUninstallInfos();
                _monitorTimer = new System.Timers.Timer();
                _monitorTimer.Interval = 3000;
                _monitorTimer.Elapsed += MonitorTimerElapsed;
                _monitorTimer.Start();
            }
            catch (Exception ex)
            {
                PPTAnnotations.Log.Error(ex);
            }
        }

        private void MonitorTimerElapsed(object sender, ElapsedEventArgs e)
        {
            _monitorTimer.Stop();
            if (IsAppCountChanged())
            {
                // 注释
            }

            _monitorTimer.Start();
        }

        /// <summary>
        /// 判断应用列表是否发生改变
        /// </summary>
        /// <returns></returns>
        public bool IsAppCountChanged()
        {
            var currentUninstallInfos = GetUninstallInfos();
            if (currentUninstallInfos.Count != _uninstallInfos.Count)
            {
                var uninstallInfoDatas = currentUninstallInfos.Select(t => t.Name).Except(_uninstallInfos.Select(t => t.Name));
                if (uninstallInfoDatas.Any(t =>
                        t.ToLower().StartsWith("wps") || t.ToLower().StartsWith("microsoft office")))
                {
                    _uninstallInfos = currentUninstallInfos;
                    return true;
                }

                _uninstallInfos = currentUninstallInfos;
                return false;
            }

            if (currentUninstallInfos.Any(item => _uninstallInfos.Find(obj => obj.Name.Equals(item.Name)) == null))
            {
                _uninstallInfos = currentUninstallInfos;
                return true;
            }

            return false;
        }

        /// <summary>从注册表中</summary>
        /// <returns></returns>
        private List<UninstallInfoData> GetUninstallInfoDatas()
        {
            List<UninstallInfoData> uninstallInfoDatas = new List<UninstallInfoData>();
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", false))
                uninstallInfoDatas.AddRange((IEnumerable<UninstallInfoData>)this.GetUninstallInfos(key));
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall", false))
                uninstallInfoDatas.AddRange((IEnumerable<UninstallInfoData>)this.GetUninstallInfos(key));
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall", false))
                uninstallInfoDatas.AddRange((IEnumerable<UninstallInfoData>)this.GetUninstallInfos(key));
            return uninstallInfoDatas;
        }