C# 4.0 启动ManagementEventWatcher时未发现异常
启动MaagementEventWatcher时,有时未发现异常 我的代码示例如下所示:C# 4.0 启动ManagementEventWatcher时未发现异常,c#-4.0,service,managementeventwatcher,C# 4.0,Service,Managementeventwatcher,启动MaagementEventWatcher时,有时未发现异常 我的代码示例如下所示: try { string scopePath = @"\\.\root\default"; ManagementScope managementScope = new ManagementScope(scopePath); WqlEventQuery query = new WqlEve
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“Not found”。可能您指定了错误的密钥路径,或者密钥路径不再可用 事实上,问题是,在笔记本电脑(具有串行端口的PC机)中,在启动首次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();
}
}
我在注册表中检查了路径或文件夹(也有项)是否可用。是的,我在事件日志事件筛选器中得到了此错误,查询“SELECT*FROM\uuuu InstanceModificationEvent WITHIN 60 WHERE TargetInstance ISA”Win32\u处理器由于错误0x80041003,无法在命名空间“///root/CIMV2”中重新激活“AND TargetInstance.LoadPercentage>99”。在更正问题之前,无法通过此筛选器传递事件。