【问题标题】:How to detect when a user is adding or removing a program in WPF C#如何检测用户何时在 WPF C# 中添加或删除程序
【发布时间】:2019-09-23 04:42:57
【问题描述】:

我的 WPF 应用程序就像一个显示应用程序的 MacBook 扩展坞。我想在用户安装或卸载程序时更新我的​​应用程序中的应用程序列表。

捕捉添加/删除程序事件的好方法是什么?

由于大多数人认为过于宽泛,我将进一步阐述:

  1. 我还没有尝试过任何东西。我唯一想到的是不时重新扫描注册表以查看程序列表是否已更改。这肯定会起作用,但是,这是我目前的备份选项,我正在寻找更好的解决方案。

  2. 我所说的已安装应用程序是指一个应用程序,该应用程序在这些位置中的任何一个位置都有与其关联的注册表项,因此它会显示在添加/删除程序窗口中。

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall

  1. 我的意思是卸载的应用程序 - 您从添加/删除程序窗口卸载的程序。

【问题讨论】:

  • 已安装的应用程序存储在注册表项中,但是您需要挂钩注册表 API、轮询注册表或可能使用 RegistryState 类
  • 您的目标实际上是不可能的,因为没有关于“安装”或“卸载”的标准。有些程序可以从拇指驱动器运行。它们是在插入 U 盘时安装的吗?卸载的时候卸载了?其他程序将自己安装到硬盘驱动器,但以非标准方式安装,这对于正常的操作系统机制是不可见的。所有主流操作系统都是如此。你的问题太宽泛了,因为它没有得到足够的约束,而且你还没有展示到目前为止你已经尝试过什么,也没有展示为什么这不能解决你的问题。
  • 大多数像您正在制作的应用程序都需要用户手动添加程序。正如 Peter 所说,自动检测已安装的应用程序在 Windows 中是一个难以解决的问题。

标签: c# wpf mvvm


【解决方案1】:

解决了。当您添加或删除程序(安装/卸载)时,Windows 会在 WindowsLogs/Application 下的事件日志(事件查看器)中写入事件。您可以使用 EventLog.EntryWritten 监听何时添加新的日志条目

EventLog eLog = new EventLog();
eLog.Log = "Application"; //MsiInstaller events are written in Application
eLog.EntryWritten += Log_NewInstallUninstallOccured; //Add the event and remove it when you want to stop listening
eLog.EnableRaisingEvents = true; // Enable event raising

private void Log_NewInstallUninstallOccured(object sender, EntryWrittenEventArgs e)
        {
            if (e.Entry.Source == "MsiInstaller") //MsiInstaller is the source responsible for installation related events
            {
                if(e.Entry.Message.Contains("Installation completed successfully."))
                {
                    Console.WriteLine("Installation Occured");
                }
                else if (e.Entry.Message.Contains("Removal completed successfully."))
                {
                    Console.WriteLine("Removal Occured");
                } else
                {
                    Console.WriteLine("Other Installation Event Occured");
                }
            }
        }

来源:

How to tell which user installed or uninstalled an app in Windows

EventLog Class

EventLog.EntryWritten Event

【讨论】:

    猜你喜欢
    • 2017-11-17
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多