通过C#和RegistryTreeChangeEvent WMI
我收到此错误:
未处理的异常:System.Runtime.InteropServices.COMException(0x80042001):来自 HRESULT 的异常:0x80042001 在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode,IntPtr errorInfo) 在 System.Management.ManagementEventWatcher.Start() at MyNamespace.Program.Main(String[] args) in {somedir}\Program.cs:line 16
这是我用来监视注册表的 C# 控制台应用程序:
using System;
using System.Management;
namespace MyNamespace
{
class Program
{
static void Main(string[] args)
{
var watcher = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM RegistryTreeChangeEvent"));
var handler = new MyHandler();
watcher.EventArrived += handler.Arrived;
//Start watching for events
watcher.Start();
while (handler.EventHasntFiredYet)
{
// Nothing.
}
//Stop watching
watcher.Stop();
}
public class MyHandler
{
public bool EventHasntFiredYet;
public MyHandler()
{
EventHasntFiredYet = true;
}
public void Arrived(object sender, EventArrivedEventArgs e)
{
var propertyDataCollection = e.NewEvent.Properties;
foreach (var p in propertyDataCollection)
{
Console.WriteLine("{0} -- {1}",p.Name,p.Value);
}
EventHasntFiredYet = false;
}
}
}
}
我试图简单地监视注册表变化。有人对为什么失败有任何建议吗?
I'm getting this error:
Unhandled Exception: System.Runtime.InteropServices.COMException (0x80042001): Exception from HRESULT: 0x80042001
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Management.ManagementEventWatcher.Start()
at MyNamespace.Program.Main(String[] args) in {somedir}\Program.cs:line 16
And here's my C# console app that I'm using to watch the registry:
using System;
using System.Management;
namespace MyNamespace
{
class Program
{
static void Main(string[] args)
{
var watcher = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM RegistryTreeChangeEvent"));
var handler = new MyHandler();
watcher.EventArrived += handler.Arrived;
//Start watching for events
watcher.Start();
while (handler.EventHasntFiredYet)
{
// Nothing.
}
//Stop watching
watcher.Stop();
}
public class MyHandler
{
public bool EventHasntFiredYet;
public MyHandler()
{
EventHasntFiredYet = true;
}
public void Arrived(object sender, EventArrivedEventArgs e)
{
var propertyDataCollection = e.NewEvent.Properties;
foreach (var p in propertyDataCollection)
{
Console.WriteLine("{0} -- {1}",p.Name,p.Value);
}
EventHasntFiredYet = false;
}
}
}
}
I'm trying to simply watch the registry for changes. Does anyone have any suggestions as to why this is failing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Hans 所说,您收到错误是因为您没有指定 where 子句。根据 为注册表提供程序创建正确的 WHERE 子句< /a> 您必须指定 where 子句,否则您将收到 WBEM_E_TOO_BROAD 错误。
为了简化代码而不是重新发明轮子,您可以使用以下库: 异步注册表通知在 .NET 中使用强类型 WMI 类
As Hans has told you are receiving the error because you haven't specified where clause. According to Creating a Proper WHERE Clause for the Registry Provider you must specify where clause otherwise you will receive WBEM_E_TOO_BROAD error.
To simplify your code and not to reinvent the wheel you can use the following library: Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET
这是一个内部 WMI 错误,
WBEMESS_E_REGISTRATION_TOO_BROAD
,“提供程序注册与系统事件域重叠。”这是您从 COM 中得到的一条很好的错误消息。令人震惊的是 .NET 异常消息有多好。不管怎样,我相当确定这意味着“你要求的事件太多了”。您必须在查询中更有选择性,使用 WHERE 子句。喜欢:
在 Giorgi 的提示下,我找到了 MSDN 页面 记录了该问题:
It is an internal WMI error,
WBEMESS_E_REGISTRATION_TOO_BROAD
, "The provider registration overlaps with the system event domain."That's about a good an error message as you'd ever get out of COM. Striking how much better the .NET exception messages are. Anyhoo, I'm fairly sure that what it means is "you are asking for WAY too many events". You'll have to be more selective in your query, use the WHERE clause. Like:
Prompted by Giorgi, I found the MSDN page that documents the problem: