Eli
Eli

Reputation: 4926

Registry Watcher C# implement RegistryKeyChangeEvent

OK, I tried this and watched this. I followed my code-

// Construct the query string:HKEY_LOCAL_MACHINE
string queryString = string.Format(@"SELECT * FROM RegistryKeyChangeEvent WHERE Hive = 'HKEY_LOCAL_MACHINE' AND KeyPath = 'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall' ");

WqlEventQuery query = new WqlEventQuery();
query.QueryString = queryString;
query.EventClassName = "RegistryKeyChangeEvent";
query.WithinInterval = new TimeSpan(0, 0, 0, 1);
this.Query = query;

this.EventArrived += new EventArrivedEventHandler(RegistryWatcher_EventArrived);

Unfortunately, when I call Start() it raise an exception - ManagementException and says "Unparsable query. "

Upvotes: 2

Views: 2418

Answers (1)

Eli
Eli

Reputation: 4926

After some workaround, I found my way to solve it once and for all with ManagementScope instead of WqlEventQuery:

ManagementScope Scope = new ManagementScope("\\\\.\\root\\default");
EventQuery Query = new EventQuery(@"SELECT * FROM RegistryKeyChangeEvent WHERE Hive='HKEY_LOCAL_MACHINE' AND KeyPath='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall'");
ManagementEventWatcher watcher = new ManagementEventWatcher(Scope, Query);
m_watcher.EventArrived += new EventArrivedEventHandler(RegistryWatcher_EventArrived);
m_watcher.Start();

It seems to be something wrong with the way it parse the query, but now its fine.

Upvotes: 2

Related Questions