时的RegistryKey值更改收到通知 [英] Receive notification when RegistryKey Value was changed

查看:556
本文介绍了时的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 = @"root\default";
_watcher.EventArrived += (sender, args) => KeyValueChanged();
_watcher.Start();



(错误是未找到)

(Error was "Not found")

我的第二个方法是使用 WBEM脚本COM组件的意图,从端口的 http://msdn.microsoft.com/en-us/library/aa393042(VS.85)的.aspx 到C#,但我没有找到的WBEM COM的任何使用示例在C#

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.

编辑:
。如果你改变蜂房 HKEY_CURRENT_USER 在MSDN VBScript的例子,它停止工作。我无法找到有关此行为,但的从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

EDIT2:
更改为 HKEY_CLASSES_ROOT HKEY_CURRENT_USER 荨麻疹不被 RegistryEvent 或其衍生类,如 RegistryValueChangeEvent 。 ( 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/Monitoring-HKEY_CURRENT_USER.aspx

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆