Getting the port name of a connected USB device

30,124

Solution 1

Maybe you can use a C# to USB device library as described here:
Are there any good C# to usb device libraries?

From the above answer: For getting a list of devices connected:
http://code.google.com/p/usb-devices-list/

Solution 2

USB ports don't have names. It is a bus, it doesn't matter what connector you use. Just like it doesn't matter where you plug in a card in the bus inside of the machine.

If you are actually talking about a USB device whose driver emulates a serial port (like "COM5"), pretty common, then you can get some info about the driver out of a WMI query, Win32_SerialPort class. Use the WMI Code Creator tool to play with such a query and to auto-generate the C# code you need.

Solution 3

COM port number can be found with help of wmi services, here is a vbscript example for detecting usb com port number

Solution 4

I found a very nice library here page 1 and here page 2

The website is Chinese, but the code is not. It does not work for x64 (it'll compile, but the answers are bogus), but does work for x86.

Share:
30,124
Zerone
Author by

Zerone

Updated on February 22, 2020

Comments

  • Zerone
    Zerone about 4 years

    When a USB device is connected to the computer, how can I get the PORT name which it is connected using C# code. I found many ways to find when a USB is connected, disconnected, drive letter, path, device id etc. but didn't find any clear example on how to know to which port it got connected. I saw one possible explanation but that involves lot of pinvokes which I totally don't understand. I like to believe there are more direct and easy ways to get such info in c# alone, without using any unmanaged code.

  • Zerone
    Zerone over 13 years
    Thank you very much for explaining the BUS. Correct; I want to get the driver emulated COM port. I'm creating an App and when I plug in my USB modem I want that to select the port automatically coz sometimes it change; right now I set it manually. Thanks for the WMI code creator it seems very useful. Can u give me any hint or help how to get the connected com ports using that, because it gives me 0 results when I try Win32_SerialPort class. Thanks