Win32_DeviceChangeEvent仅报告发生的事件类型和事件时间(uint64,表示 1601 年 1 月 1 日之后的 100 纳秒间隔,UTC)。如果您还想知道到达或删除的内容,则没有多大用处。
我建议改用WqlEventQuery类,将其EventClassName设置为__InstanceOperationEvent。
该系统类提供了一个TargetInstance
可以转换为ManagementBaseObject的属性,该对象是完整的管理对象,还提供有关生成事件的设备的基本信息。
在这些信息(包括设备的友好名称)中,PNPDeviceID
可用于构建其他查询以进一步检查引用的设备。
的Condition属性可以在这里设置WqlEventQuery
为。
它可以设置为任何其他感兴趣的类别。TargetInstance ISA 'Win32_DiskDrive'
Win32_
设置事件监听器(本地机器):(
调用事件处理程序DeviceChangedEvent
)
var query = new WqlEventQuery() {
EventClassName = "__InstanceOperationEvent",
WithinInterval = new TimeSpan(0, 0, 3),
Condition = @"TargetInstance ISA 'Win32_DiskDrive'"
};
var scope = new ManagementScope("root\\CIMV2");
using (var moWatcher = new ManagementEventWatcher(scope, query))
{
moWatcher.Options.Timeout = ManagementOptions.InfiniteTimeout;
moWatcher.EventArrived += new EventArrivedEventHandler(DeviceChangedEvent);
moWatcher.Start();
}
事件处理程序在 中接收e.NewEvent.Properties["TargetInstance"]
表示Win32_DiskDrive类的管理对象。
请参阅有关此处直接可用的属性的文档。
报告的感兴趣的__InstanceOperationEvent
派生类e.NewEvent.ClassPath.ClassName
可以是:
__InstanceCreationEvent:检测到新设备到达。
__InstanceDeletionEvent:检测到设备移除。
__InstanceModificationEvent:现有设备已以某种方式进行了修改。
请注意,该事件是在辅助线程中引发的,我们需要BeginInvoke()
UI 线程使用新信息更新 UI。
▶ 你应该避免Invoke()
在这里,因为它是同步的:它会阻塞处理程序直到方法完成。此外,在这种情况下,死锁并不是一种遥远的可能性。
请参阅此处:获取 USB 存储设备的序列号,该类提供有关设备的大部分可用信息(信息被过滤以仅显示 USB 设备,但可以删除过滤器)。
private void DeviceChangedEvent(object sender, EventArrivedEventArgs e)
{
using (var moBase = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value)
{
string oInterfaceType = moBase?.Properties["InterfaceType"]?.Value.ToString();
string devicePNPId = moBase?.Properties["PNPDeviceID"]?.Value.ToString();
string deviceDescription = moBase?.Properties["Caption"]?.Value.ToString();
string eventMessage = $"{oInterfaceType}: {deviceDescription} ";
switch (e.NewEvent.ClassPath.ClassName)
{
case "__InstanceDeletionEvent":
eventMessage += " removed";
BeginInvoke(new Action(() => UpdateUI(eventMessage)));
break;
case "__InstanceCreationEvent":
eventMessage += "inserted";
BeginInvoke(new Action(() => UpdateUI(eventMessage)));
break;
case "__InstanceModificationEvent":
default:
Console.WriteLine(e.NewEvent.ClassPath.ClassName);
break;
}
}
}
private void UpdateUI(string message)
{
//Update the UI controls with information provided by the event
}