我想要在 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模板网!
LINQ to SQL 和并发问题LINQ to SQL and Concurrency Issues(LINQ to SQL 和并发问题)
try/catch 块的收益回报Yield return from a try/catch block(try/catch 块的收益回报)
重用带有事务的 SqlCommand 时,我应该调用 ParameShould I call Parameters.Clear when reusing a SqlCommand with a transation?(重用带有事务的 SqlCommand 时,我应该调用 Parameters.Clear 吗
处理时不带变量的 using 语句有什么作用?what does a using statement without variable do when disposing?(处理时不带变量的 using 语句有什么作用?)
为什么 TransactionScope 不适用于实体框架?Why doesn#39;t TransactionScope work with Entity Framework?(为什么 TransactionScope 不适用于实体框架?)
如何在可取消的 async/await 中处理 TransactionScope?How to dispose TransactionScope in cancelable async/await?(如何在可取消的 async/await 中处理 TransactionScope?)