RegistryTreeChangeEvent through C # and WMI

I get this error:

Unhandled exception: System.Runtime.InteropServices.COMException (0x80042001): Exception from HRESULT: 0x80042001 in System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal (Int32 errorCode, IntPtr errorInfo) in System.Mangram.Mangram. .Main (String [] args) in {somedir} \ Program.cs: line 16

And here is my C # console application that I use to view 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 just watch the registry for changes. Does anyone have any suggestions as to why this fails?

+3
source share
2 answers

WMI, WBEMESS_E_REGISTRATION_TOO_BROAD, " ".

, - COM. , .NET. Anyhoo, , , " WAY ". , WHERE. :

SELECT * FROM RegistryTreeChangeEvent
WHERE Hive = 'HKEY_LOCAL_MACHINE' AND 'RootPath =' SOFTWARE\Microsoft


Giorgi, MSDN, :

.

SELECT * FROM RegistryTreeChangeEvent WHERE hive = hkey_local_machine " rootpath =" "

, WMI WBEM_E_TOO_BROAD , WHERE, WHERE , . < /" >

+4

, , , . WHERE , WBEM_E_TOO_BROAD.

, : WMI .NET

+1

Source: https://habr.com/ru/post/1757002/


All Articles