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?