简体   繁体   English

WMI:在插入时获取 USB 设备描述

[英]WMI: Get USB device description on insertion

How can I get a device Id and other description on insertion of USB device?如何获取有关插入 USB 设备的设备 ID 和其他描述? I've found an example how to get notified about USB device insertion/removal.我找到了一个示例,如何获得有关 USB 设备插入/移除的通知。 But how to get device desrtiption info?但是如何获取设备描述信息呢?

Here is my code snippet:这是我的代码片段:

WqlEventQuery q;
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true;

try
{
    q = new WqlEventQuery();
    q.EventClassName = "__InstanceDeletionEvent";
    q.WithinInterval = new TimeSpan(0, 0, 3);
    q.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
    w = new ManagementEventWatcher(scope, q);
    w.EventArrived += new EventArrivedEventHandler(USBRemoved);
    w.Start();
}
... catch()....

UPDATE: Actually, it is a Serial COM device with USB connection.更新:实际上,它是带有 USB 连接的串行 COM 设备。 So there is no driveName property.所以没有 driveName 属性。 How can I get USB description, which I can see in Device Manager?如何获得 USB 描述,我可以在设备管理器中看到? Does WMI provide this info with the notification about USB insertion? WMI 是否将此信息与有关 USB 插入的通知一起提供?

Complete new answer according to your updated answer.根据您更新的答案完成新的答案。 You may check für any connected USB device :您可以检查任何连接的 USB 设备

        ManagementScope sc =
            new ManagementScope(@"\\YOURCOMPUTERNAME\root\cimv2");

        ObjectQuery query =
            new ObjectQuery("Select * from Win32_USBHub");

        ManagementObjectSearcher searcher = new ManagementObjectSearcher(sc, query);
        ManagementObjectCollection result = searcher.Get();

        foreach (ManagementObject obj in result)
        {
            if (obj["Description"] != null) Console.WriteLine("Description:\t" + obj["Description"].ToString());
            if (obj["DeviceID"] != null) Console.WriteLine("DeviceID:\t" + obj["DeviceID"].ToString());
            if (obj["PNPDeviceID"] != null) Console.WriteLine("PNPDeviceID:\t" + obj["PNPDeviceID"].ToString());
        }

(see MSDN WMI tasks examples ) for this) (请参阅MSDN WMI 任务示例

or have a look into any COM ConnectedDevice或查看任何COM ConnectedDevice

        ManagementScope sc =
            new ManagementScope(@"\\YOURCOMPUTERNAME\root\cimv2");
        ObjectQuery query =
            new ObjectQuery("Select * from Win32_SerialPort");

        searcher = new ManagementObjectSearcher(sc, query);
        result = searcher.Get();

        foreach (ManagementObject obj in result)
        {
            if (obj["Caption"] != null) Console.WriteLine("Caption:\t" + obj["Description"].ToString());
            if (obj["Description"] != null) Console.WriteLine("Description:\t" + obj["DeviceID"].ToString());
            if (obj["DeviceID"] != null) Console.WriteLine("DeviceID:\t" + obj["PNPDeviceID"].ToString());
        }

(see ActiveX Experts for further details on this) (有关这方面的更多详细信息,请参阅ActiveX 专家

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM