WMI:获取上插入USB设备描述 [英] WMI: Get USB device description on insertion

查看:1158
本文介绍了WMI:获取上插入USB设备描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能得到一个设备ID和其他说明在插入USB设备? 我找到了一个例子,如何得到通知有关USB设备插入/拔出。可是如何才能让设备desrtiption信息?

How can I get a device Id and other description on insertion of USB device? I've found an example how to get notified about USB device insertion/removal. But how to get device desrtiption info?

下面是我的code片断:

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()....

更新:其实,这是具有USB连接的串行COM设备。所以没有DRIVENAME属性。我怎样才能获得USB的描述,我可以在设备管理器中看到了什么?是否WMI提供有关USB插入通知这个信息?

UPDATE: Actually, it is a Serial COM device with USB connection. So there is no driveName property. How can I get USB description, which I can see in Device Manager? Does WMI provide this info with the notification about USB insertion?

推荐答案

完全新的答案根据更新后的答案。您可以检查献给所有的连接USB设备的:

Complete new answer according to your updated answer. You may check für any connected USB device:

        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());
        }

(见 MSDN WMI任务范例)本)

或看看到任何的 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());
        }

(见的ActiveX专家了解更多相关信息)

(see ActiveX Experts for further details on this)

这篇关于WMI:获取上插入USB设备描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆