4

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.

Vikram Bose
  • 3,197
  • 2
  • 16
  • 33

2 Answers2

2

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.

Suresh Kumar Veluswamy
  • 4,193
  • 2
  • 20
  • 35
  • I checked in the registry the path or folder(Key also) are available. – Vikram Bose May 02 '13 at 06:20
  • Yes, I get this Error, Given Below in the Event logEvent filter with query "SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA "Win32_Processor" AND TargetInstance.LoadPercentage > 99" could not be reactivated in namespace "//./root/CIMV2" because of error 0x80041003. Events cannot be delivered through this filter until the problem is corrected. – Vikram Bose May 06 '13 at 09:06
2

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();
                }
            }
Vikram Bose
  • 3,197
  • 2
  • 16
  • 33