在 RegistryKey 值更改时收到通知
- 作者: 嗫?暁雲?
- 来源: 51数据库
- 2022-10-20
问题描述
我想要在 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://www.51sjk.com/Upload/Articles/1/0/331/331949_20221020120524229.aspx 到 c# 但我没有找到c# 中 WBEM COM 的任何使用示例
My second approach was using the WBEM Scripting COM component with the intent to port the example from http://www.51sjk.com/Upload/Articles/1/0/331/331949_20221020120524229.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
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
