Not found exception When start the ManagementEventWatcher

3.5k Views Asked by At

Not Found Exception some times while starting the MaagementEventWatcher

My code sample is given 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();
            }
        }

Exception:

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

Note : I checked in the registry,folder and files are found.

2

There are 2 best solutions below

0
On BEST ANSWER

Actually the problem was, In the laptops(pcs having the serial ports so COM1 port) when starting first time SERIALCOMM folder not created in the Registry because,

basically we plugged the device in the USB port or serial port the SERIALCOMM folder will create, in this case we are using WMI to get the connected comm ports from the registry.

in some laptops no USB ports and Serial Ports are connected So, the SERIALCOMM folder not created, In that time we are accessing this registry path we get the error.

so the Solution is,

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
On

ManagementException "Not found" is thrown when there is not match in the WQL query. Maybe you have specified the wrong KeyPath or the KeyPath is no longer available.