evans
evans

Reputation: 23

How to list USB locationinfo for all USB devices

I'm trying to find a way find specific slot no of a USB Hub using dotnet (vb or c#) I can see that this information exists in device manager as:

Port_#0001.Hub_#0004

In short I would like to list all USB Ports of the hub and the device name that is connected to the hub. I was checking the WMI option but I cannot find the Device_LocationInfo there. Can you please help?

Upvotes: 2

Views: 2107

Answers (1)

Precious Uwhubetine
Precious Uwhubetine

Reputation: 3007

The Win32PnPSignedDriver class Contains Information about not only usb devices but all devices connected to your PC. It has a field Location which contains what you may be looking for. Let's Begin Like this. I would be working in VB.

Create a New Windows Form, and add A button(Button1) and a DataGridView(DataGridView1).

From Visual studio, go to Project > Add References and add a Reference to System.Management

Open Form1.vb and enter the following code

Imports System.Management
Imports Microsoft.Win32
Public Class Form1
    Dim dt As New DataTable

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT DeviceName, Location, Manufacturer FROM Win32_PnPSignedDriver")
        dt.Columns.Clear()
        For Each queryObj As ManagementObject In searcher.Get()
            For Each item As PropertyData In queryObj.Properties()
                Try
                    dt.Columns.Add(item.Name)
                Catch ex As Exception

                End Try
            Next
            Exit For
        Next
        dt.Rows.Clear()
        For Each queryObj As ManagementObject In searcher.Get()
            Dim dr As DataRow = dt.NewRow
            For Each item As PropertyData In queryObj.Properties
                Try
                    dr(item.Name) = item.Value
                Catch ex As Exception

                End Try
            Next
            dt.Rows.Add(dr)
            dr = dt.NewRow
         Next
         DataGridView1.DataSource = dt
    End Sub
End Class

When You compile the above code and run it, You get the following output or something similar. enter image description here

Upvotes: 1

Related Questions