問題描述
我想要在 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_ROOT
和 HKEY_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模板網!