WMI: Get USB device description when inserted

How can I get the device ID and other description when inserting a USB device? I found an example of how to get a notification about inserting / removing USB devices. But how to get information about the device’s despatch?

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: This is actually a Serial COM device with a USB connection. Therefore, the driveName property is missing. How can I get the USB description that I see in the device manager? Does WMI provide this USB insert notification?

+5
source share
1 answer

Fill out a new answer according to your updated answer. You can check the 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 Task Examples ) for this)

or view 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 more information on this)

+13
source

Source: https://habr.com/ru/post/949235/


All Articles