C# How to find out particular USB device from multiple connected same USB device?

1.5k Views Asked by At

I have same 2 Logitech webcam devices (c920) . I want to assign this 2 devices into IN-Camera and OUT-Camera since i need transaction of the IN and OUT users.

Now, when i connect the same into the PC. the VID, PID are same since the OEM also same.

So How can i find out the 2 devices each time it's plugged into the PC or after PC restarted by any reason.

So I am looking for some UNIQUE USB descriptor for this USB device's.

need Help on this.

1

There are 1 best solutions below

0
Axel Kemper On

The following code might help to access the hardware IDs of your USB devices:

using System;
using System.Management;

namespace akWmiDeviceDemo
{
    class Program
    {
        static void Main(string[] args)
        {
        //  inspired by:
        //  https://blogs.msdn.microsoft.com/powershell/2007/02/24/displaying-usb-devices-using-wmi/
            string strComputer = ".";
            ManagementScope scope = new ManagementScope(@"\\" + strComputer + @"\root\cimv2");
            ObjectQuery queryUsbControllers = new ObjectQuery("Select * From Win32_USBControllerDevice");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, queryUsbControllers);
            ManagementObjectCollection usbControllers = searcher.Get();

            foreach (ManagementObject usbController in usbControllers)
            {
                string dependent = (string)usbController["Dependent"];
                string[] names = dependent.Replace("\"", "").Split(new char [] {'='});
                string strUsbControllerName = names[1];
                ObjectQuery queryUsbDevices = new ObjectQuery("Select * From Win32_PnPEntity Where DeviceID = '" + strUsbControllerName + "'");
                ManagementObjectSearcher deviceSearcher = new ManagementObjectSearcher(scope, queryUsbDevices);
                ManagementObjectCollection usbDevices = deviceSearcher.Get();

                o("");
                o("USB controller = {0}", strUsbControllerName);
                foreach (ManagementObject usbDevice in usbDevices)
                {
                    o("description = {0}", usbDevice["Description"]);
                    o("PnPDeviceID = {0}", usbDevice["PnPDeviceID"]);
                }
            }
        }

        static void o(string format, params object[] args)
        {
            Console.WriteLine(format, args);
        }
    }
}