WMI: Get USB device description on insertion

前端 未结 1 1854
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 05:15

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 dev

相关标签:
1条回答
  • 2020-12-18 05:30

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

    (see MSDN WMI tasks examples) for this)

    or have a look into any 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)

    0 讨论(0)
提交回复
热议问题