【问题标题】:Not found exception When start the ManagementEventWatcher启动 ManagementEventWatcher 时未找到异常
【发布时间】:2023-03-28 06:17:01
【问题描述】:

启动 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()]

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

【问题讨论】:

    标签: c#-4.0 service managementeventwatcher


    【解决方案1】:

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

    【讨论】:

    • 我在注册表中检查了路径或文件夹(密钥也可用)。
    • 是的,我收到此错误,在事件日志事件过滤器中给出以下查询“SELECT * FROM __InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA "Win32_Processor" AND TargetInstance.LoadPercentage > 99" 无法在命名空间中重新激活“//./root/CIMV2”,因为错误 0x80041003。在问题得到纠正之前,无法通过此过滤器传递事件。
    【解决方案2】:

    实际上的问题是,在笔记本电脑(具有串口所以 COM1 端口的个人电脑)中,第一次启动时没有在注册表中创建 SERIALCOMM 文件夹,因为,

    基本上我们将设备插入到 SERIALCOMM 文件夹将创建的 USB 端口或串行端口中,在这种情况下,我们使用 WMI 从注册表中获取连接的通信端口。

    在某些笔记本电脑中,没有连接 USB 端口和串行端口因此,未创建 SERIALCOMM 文件夹,在我们访问此注册表路径时,我们会收到错误消息。

    所以解决方案是,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 2023-03-04
      • 2013-05-06
      • 2019-07-27
      相关资源
      最近更新 更多