当USB设备与Win32_DeviceChangeEvent连接时,我可以捕捉到
但是只允许查看3个属性
class Win32_DeviceChangeEvent : __ExtrinsicEvent
{
uint8 SECURITY_DESCRIPTOR[];
uint64 TIME_CREATED;
uint16 EventType;
};
但我不明白如何获取有关此设备的所有信息。具体而言,其端口和集线器,VirtualHubAdress名称等。
public enum EventType
{
Inserted = 2,
Removed = 3
}
public static void RegisterUsbDeviceNotification()
{
var watcher = new ManagementEventWatcher();
var query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
//watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.EventArrived += (s, e) =>
{
//here is im need to get info about this device
EventType eventType = (EventType)(Convert.ToInt16(e.NewEvent.Properties["EventType"].Value));
};
watcher.Query = query;
watcher.Start();
}
也许我可以用这样的东西做
[DllImport("UseFull.dll")]
private IntpPtr GetAllinfo(params);
该Win32_DeviceChangeEvent(1601,UTC 1月1日后UINT64,代表100纳秒的时间间隔)报告所发生的事件只是类型和事件的时间。如果您还想知道到达或删除的内容,则没有什么用。
我建议改用WqlEventQuery类,将其设置EventClassName
为[ __InstanceOperationEvent
] [4]。
该系统类提供了一个TargetInstance
属性,可以将其强制转换为ManagementBaseObject,完整的管理对象还提供有关生成事件的Device的基本信息。
在这些信息(包括设备的友好名称)中,PNPDeviceID
可以用于构建其他查询以进一步检查引用的设备。
该WqlEventQuery
的条件属性可以在这里设置到TargetInstance ISA 'Win32_DiskDrive'
。
可以将其设置为其他任何Win32_
感兴趣的类别。
设置事件侦听器(本地计算机):(
事件处理程序称为DeviceChangedEvent
)
WqlEventQuery query = new WqlEventQuery() {
EventClassName = "__InstanceOperationEvent",
WithinInterval = new TimeSpan(0, 0, 3),
Condition = @"TargetInstance ISA 'Win32_DiskDrive'"
};
ManagementScope scope = new ManagementScope("root\\CIMV2");
using (ManagementEventWatcher moWatcher = new ManagementEventWatcher(scope, query))
{
moWatcher.Options.Timeout = ManagementOptions.InfiniteTimeout;
moWatcher.EventArrived += new EventArrivedEventHandler(DeviceChangedEvent);
moWatcher.Start();
}
事件处理程序在中接收e.NewEvent.Properties["TargetInstance"]
代表[Win32_DiskDrive] [7]类的管理对象。
有关可直接在此处使用的属性的信息,请参阅文档。
所__InstanceOperationEvent
报告的感兴趣的派生类别e.NewEvent.ClassPath.ClassName
可以是:
__InstanceCreationEvent:检测到新的设备到达。
__InstanceDeletionEvent:已检测到设备移除。
__InstanceModificationEvent:现有设备已通过某种方式进行了修改。
请注意,该事件是在辅助线程中引发的,我们需要BeginInvoke
UI线程使用新信息来更新UI。
请参阅此处:获取提供有关设备的大多数可用信息的类的USB存储设备的序列号(信息经过过滤以仅显示USB设备,但是可以删除过滤器)。
private void DeviceChangedEvent(object sender, EventArrivedEventArgs e)
{
using (ManagementBaseObject 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";
this.BeginInvoke(new MethodInvoker(() => { this.UpdateUI(eventMessage); }));
break;
case "__InstanceCreationEvent":
eventMessage += "inserted";
this.BeginInvoke(new MethodInvoker(() => { this.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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句