在 RegistryKey 值更改时收到通知

时间:2022-12-03
本文介绍了在 RegistryKey 值更改时收到通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在 HKEY_CURRENT_USER 中的特定 RegistryKey 更改时收到通知.到目前为止,我通过 WMI 尝试了这个,但没有成功:

I want a notification when a specific RegistryKey in HKEY_CURRENT_USER is changed. So far I tried this via WMI with no success:

var query = new WqlEventQuery(string.Format(
"SELECT * FROM RegistryKeyChangeEvent WHERE Hive='{0}' AND KeyPath='{1}' AND ValueName='{2}'",
                hive, keyPath.Replace("\","\\"), valueName));
_watcher = new ManagementEventWatcher(query);
_watcher.Scope.Path.NamespacePath = @"rootdefault";
_watcher.EventArrived += (sender, args) => KeyValueChanged();
_watcher.Start();

(错误是未找到")

我的第二种方法是使用 WBEM Scripting COM 组件,目的是移植来自 http://msdn.microsoft.com/en-us/library/aa393042(VS.85).aspx 到 c# 但我没有找到c# 中 WBEM COM 的任何使用示例

My second approach was using the WBEM Scripting COM component with the intent to port the example from http://msdn.microsoft.com/en-us/library/aa393042(VS.85).aspx to c# but I didn't find any usage samples for the WBEM COM in c#

我发现了这个 http://www.codeproject.com/KB/system/registrymonitor.aspx 类,但它不符合我的需要,因为该类只监视整个键,我只想要一个特定值(通过上面示例中的 ValueName 指定)时的通知) 发生变化.

I found this http://www.codeproject.com/KB/system/registrymonitor.aspx class, but it didn't fit my needs as this class only monitors the whole key and I only want a notification when a specific value (specified via the ValueName in the samples above) gets changed.

如果您在 msdn vbscript 示例中将 Hive 更改为 HKEY_CURRENT_USER,它将停止工作.我找不到有关此行为的任何信息,但是 2003 年的链接

If you change the Hive to HKEY_CURRENT_USER in the msdn vbscript example, it stops working. I couldn't find anything about this behaviour but a link from 2003

RegistryEvent 或从它派生的类(例如 RegistryValueChangeEvent)不支持对 HKEY_CLASSES_ROOTHKEY_CURRENT_USER 配置单元的更改.(MSDN)

Changes to the HKEY_CLASSES_ROOT and HKEY_CURRENT_USER hives are not supported by RegistryEvent or classes derived from it, such as RegistryValueChangeEvent. (MSDN)

推荐答案

我终于解决了问题,让 WMI 查询版本可以工作:

I finally solved the problem and got the WMI query version to work:

var currentUser = WindowsIdentity.GetCurrent();
var query = new WqlEventQuery(string.Format(
"SELECT * FROM RegistryValueChangeEvent WHERE Hive='HKEY_USERS' AND KeyPath='{0}\\{1}' AND ValueName='{2}'",
currentUser.User.Value, keyPath.Replace("\","\\"), valueName));
_watcher = new ManagementEventWatcher(query);
_watcher.EventArrived += (sender, args) => KeyValueChanged();
_watcher.Start();

我在 http://www.codeproject.com/Messages/2844468/监控-HKEY_CURRENT_USER.aspx

这篇关于在 RegistryKey 值更改时收到通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:来自 Windows 10 桌面应用程序的吐司通知中的意外 下一篇:通知窗口 - 防止窗口获得焦点

相关文章

最新文章