Rahul Arya
Rahul Arya

Reputation: 1

Detect USB Ports in the Machine using C#

I have 3 USB 'Type A' USB slots in my laptop. I am trying to Detect the three slots but I'm not able to. This is the code that i am using to detect the ports on my Laptop. But this code returns all the Ports that are there in the laptop.

public static List<USBPort> GetUSBPortsDetails()
{
    var list = new List<USBPort>();
    Console.WriteLine("USB Port Details:");

    // Create a WMI query to get information about USB hubs
    var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_USBHub") ?? throw new NoNullAllowedException();

    // Iterate through each USB hub found
    foreach (ManagementObject usbHub in searcher.Get().Cast<ManagementObject>())
    {
        // Display details for each USB port or hub
        Console.WriteLine("------------------------------------------------");
        Console.WriteLine($"Device ID: {usbHub["DeviceID"]}");
        Console.WriteLine($"Description: {usbHub["Description"]}");
        Console.WriteLine($"PNP Device ID: {usbHub["PNPDeviceID"]}");
        Console.WriteLine($"Status: {usbHub["Status"]}");

        USBPort port = new(usbHub["Description"].ToString() ?? string.Empty,
        usbHub["DeviceID"].ToString() ?? string.Empty,
        usbHub["PNPDeviceID"].ToString() ?? string.Empty, usbHub["Status"].ToString()
        ?? string.Empty, string.Empty, string.Empty, string.Empty);

        // Default to empty if VID or PID is not found
        string vid = string.Empty;
        string pid = string.Empty;

        // Look for "VID_" and "PID_" in the string
        int vidIndex = port.PNPDeviceID.IndexOf("VID_");
        int pidIndex = port.PNPDeviceID.IndexOf("PID_");

        if (vidIndex >= 0)
        {
            // Extract 4 characters following "VID_"
            vid = port.PNPDeviceID.Substring(vidIndex + 4, 4);
        }

        if (pidIndex >= 0)
        {
            // Extract 4 characters following "PID_"
            pid = port.PNPDeviceID.Substring(pidIndex + 4, 4);
        }


        // Attempt to extract the port number if available in Location Path
        var locationPath = usbHub["PNPDeviceID"]?.ToString();
        if (locationPath != null && locationPath.Contains("USB"))
        {
            // Extract port number from the Location Path
            var portInfo = locationPath.Split('\\');
            foreach (var part in portInfo)
            {
                if (part.StartsWith("USB"))
                {
                    port = port with { VID = vid, PID = pid, PortNumber = part };
                    Console.WriteLine($"Port Number: {part}");
                    Console.WriteLine($"VID: {vid}");
                    Console.WriteLine($"PID: {pid}");
                    break;
                }
            }
        }
        Console.WriteLine("------------------------------------------------\n");
        list.Add(port);
    }
    return list;
}

I want to return the 3 usb slots specifically the Type A slots.

Upvotes: 0

Views: 40

Answers (0)

Related Questions