Exception not found When starting ManagementEventWatcher

Not found Exception several times when starting MaagementEventWatcher

My sample code is below:

try { string scopePath = @"\\.\root\default"; ManagementScope managementScope = new ManagementScope(scopePath); WqlEventQuery query = new WqlEventQuery( "SELECT * FROM RegistryKeyChangeEvent WHERE " + "Hive = 'HKEY_LOCAL_MACHINE'" + @"AND KeyPath = 'HARDWARE\\DEVICEMAP\\SERIALCOMM'"); registryWatcher = new ManagementEventWatcher(managementScope, query); registryWatcher.EventArrived += new EventArrivedEventHandler(SerialCommRegistryUpdated); registryWatcher.Start(); } catch (Exception ex) { Console.WriteLine(ex.Message); if (registryWatcher != null) { registryWatcher.Stop(); } } 

An exception:

  Not found at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) at System.Management.ManagementEventWatcher.Start() at MTTS.LabX.RockLog.AppService.USBMonitor.AddRegistryWatcherHandler()] 

Note. I registered in the registry, folder and files.

+4
source share
2 answers

In fact, the problem was that on laptops (PCs with serial ports, such as COM1 port) the first time you run the SERIALCOMM folder that was not created in the registry, because

we basically connected the device to a USB port or a serial port created by the SERIALCOMM folder, in which case we use WMI to connect the connected communication ports from the registry.

some laptops do not have USB and serial ports connected. Therefore, the SERIALCOMM folder is not created. During this time, we gain access to this registry path and get an error.

so there’s a solution,

 try { string scopePath = @"\\.\root\default"; ManagementScope managementScope = new ManagementScope(scopePath); string subkey = "HARDWARE\\DEVICEMAP\\SERIALCOMM"; using (RegistryKey prodx = Registry.LocalMachine) { prodx.CreateSubKey(subkey); } WqlEventQuery query = new WqlEventQuery( "SELECT * FROM RegistryKeyChangeEvent WHERE " + "Hive = 'HKEY_LOCAL_MACHINE'" + @"AND KeyPath = 'HARDWARE\\DEVICEMAP\\SERIALCOMM'"); registryWatcher = new ManagementEventWatcher(managementScope, query); registryWatcher.EventArrived += new EventArrivedEventHandler(SerialCommRegistryUpdated); registryWatcher.Start(); } catch (Exception ex) { Console.WriteLine(ex.Message); if (registryWatcher != null) { registryWatcher.Stop(); } } 
+2
source

A ManagementException "Not Found" is raised if there is no match in the WQL query. You may have specified the wrong KeyPath or KeyPath is no longer available.

+2
source

All Articles