-3

I am trying to find a way to read back some information of a USB port after detecting a DBT_DEVICEARRIVAL event. [c#]

In Windows, through the device manager i am able to read back a lot of information:

Device Manager -> USB Serial Port -> Properties -> Details -> Property -> xxxx (e.g Manufacturer)

Can anyone please tell me how to access that information programatically in c#?

Thanks for your time!

  • I googled "c# read usb manufacturer" and got plenty of examples. Any reason those don't work for you? – mammago Nov 08 '17 at 15:14
  • Despite being VB.Net, this might help: https://stackoverflow.com/questions/41782308/acces-device-information-with-c-sharp-or-vb – Diado Nov 08 '17 at 15:16

1 Answers1

0

Quick snippet to get you started

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");
  foreach (ManagementObject queryObj in searcher.Get()) {
      ....
  }

Check the documentation for the instance values, for example queryObj["InstanceName"].ToString() often includes the manufacturers or/and model name.

Rob
  • 444
  • 3
  • 10
  • Thanks Rob, that got me started. I ended up using win32_PnPEntity to pick up the port I was after. However i do face another issue (with a workaround). I am trying to detect USB plug-in/out events to see whether a certain piece of hardware is being connected or not. I seem to get an InvalidCastException if i query win32_PnPEntity immediately after the event. I have found that i need to wait 5000ms minimum and that clears the issue. Do you know of any cleaner workaround? – AlkiZaganiaris Nov 08 '17 at 18:21
  • not worked with that event so no workaround I've tried (the serial devices I used were pretty much always plugged in). Perhaps try-catch in a loop testing every 500 or 1000ms (of course set a maximum in the loop for the number of attempts). – Rob Nov 09 '17 at 12:17