如何获取最近连接的USB设备的信息?

问题描述 投票:2回答:3

我可以通过以下方法获取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);
c# windows wmi
3个回答
2
投票

Win32_DeviceChangeEvent 只报告事件发生的类型和事件发生的时间(uint64,代表1601年1月1日以后100纳秒的时间间隔)。如果你还想知道什么东西到达了或者被删除了,那就没那么有用了。

我建议使用 WqlEventQuery 类,设置其 EventClassName__InstanceOperationEvent. 该系统类提供了一个 TargetInstance 属性,可以投向 管理基础对象,完整的管理对象,还提供了产生该事件的设备的基础信息。在这些信息中(包括设备的友好名称),其中的 PNPDeviceID,它可以用来建立其他查询以进一步检查所引用的设备。

WqlEventQuery's 状况 这里可以设置为 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 类。请看这里直接提供的关于属性的文档。

__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 the updated informations
}

0
投票

你可以尝试使用Win32_PnPEntity来获取详细信息。Win32_PnPEntity类


0
投票

您可以使用 ORMi 来创建一个监视者,这样你就可以获得任何新设备的信息。

WMIHelper helper = new WMIHelper("root\\CimV2");

WMIWatcher watcher = new WMIWatcher("root\\CimV2", "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_PnPEntity'");
watcher.WMIEventArrived += Watcher_WMIEventArrived;

然后你就可以观看事件。

private static void Watcher_WMIEventArrived(object sender, WMIEventArgs e)
{
    //DO YOUR WORK
}
© www.soinside.com 2019 - 2024. All rights reserved.