C#如何从多个连接的同一USB设备中找出特定的USB设备?

时间:2017-03-29 06:36:08

标签: c# usb webcam-capture usb-descriptor

我有2台Logitech网络摄像头设备(c920)。我想将这2台设备分配到IN-Camera和OUT-Camera,因为我需要IN和OUT用户的交易。

现在,当我将它连接到PC时。 VID,PID是相同的,因为OEM也一样。

那么,每次插入电脑或电脑重启后,我怎样才能找到2台设备。

所以我正在为这款USB设备寻找一些独特的USB描述符。

需要帮助。

1 个答案:

答案 0 :(得分:0)

以下代码可能有助于访问USB设备的硬件ID:

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);
        }
    }
}