启动MaagementEventWatcher时有时未找到异常

我的代码示例如下:

 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();
            }
        }


例外:

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


注意:我检查了注册表,找到了文件夹和文件。

最佳答案

WQL查询中没有匹配项时,将引发ManagementException“未找到”。也许您指定了错误的KeyPath或KeyPath不再可用。

08-17 14:52