How do I find the number of USB ports available, and what their references are.
Example there are 6 USB ports on my motherboard what code would I use to find their reference like, port 1 is called 0 and port 2 is called 1.
I want a user to select a designated port and save the selection to a config file so that whenever the user plugs in a removable diskdrive into that specific port a dialogue box will popup asking the user if they want to backup their designated folder (path will also be saved in config file) to that removable diskdrive.
I know how to do everything like copying the folder to the diskdrive and all that I just need a code snippet to find the system reference to the USB ports.

Dani AI

Generated

Short answer for : Windows does not expose a simple managed “port 0 / port 1” you can read directly. USB is exposed as hubs and hub ports; to reliably detect “physical port X” you must query the USB hub topology (native SetupAPI / usbioctl calls or the equivalent) and store a hub identifier + port index — not a drive letter.

Recommended workflow:

  • Enumerate USB hubs and their ports, query each port's connection info to get the device instance/location path.
  • Present hub+port entries in the UI so the user picks the physical port; save the hub path + port index in config.
  • Subscribe to device arrival (WM_DEVICECHANGE or a WMI watcher). On arrival, resolve the incoming device to its parent hub and port and compare to the saved hub+port; if it matches, show your backup dialog.

A minimal VB.NET hook for arrivals (WinForms) — the native resolution step is where you call SetupAPI/DeviceIoControl to get hub+port details:

Protected Overrides Sub WndProc(ByRef m As Message)
    Const WM_DEVICECHANGE As Integer = &H219
    Const DBT_DEVICEARRIVAL As Integer = &H8000
    Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004

    If m.Msg = WM_DEVICECHANGE Then
        Dim wParam = m.WParam.ToInt32()
        If wParam = DBT_DEVICEARRIVAL Then
            HandleDeviceArrival(m.LParam)
        ElseIf wParam = DBT_DEVICEREMOVECOMPLETE Then
            HandleDeviceRemoval(m.LParam)
        End If
    End If
    MyBase.WndProc(m)
End Sub

Notes and gotchas: hub+port indexes are relative to a hub — external hubs change numbering. Some USB-to-SATA bridges or enclosures do not expose unique serials, so also saving a device serial or volume serial (when available) as a fallback improves reliability. Many of the API calls require P/Invoke and a bit of native plumbing (SetupDi, CM_*/DeviceIoControl). As pointed out, MSDN has concrete native examples; combine those examples with the WM_DEVICECHANGE approach above and store hub-path + port-index in config for stable behavior.

Recommended Answers

All 4 Replies

Here is a good reference I used when I did a small project with USB manipulation.

I have not currently found any classes or declarations there that might help but I'll check tonight before I mark as solved.
Thanx for the reply though.

I will do some research to see if I can find some other references for you.

I have a class for RS232 still, but I don't think that will help you any. :P

EDIT

Or would it....

Does anyone know if RS232 API will work with USB?

They are both "serial" ports.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.