1

我有相同的 2 个罗技网络摄像头设备 (c920)。我想将这两个设备分配到 IN-Camera 和 OUT-Camera,因为我需要 IN 和 OUT 用户的事务。

现在,当我将其连接到 PC 时。VID、PID 相同,因为 OEM 也相同。

那么,每次将其插入 PC 或因任何原因重新启动 PC 后,我如何才能找出这 2 个设备。

所以我正在为这个 USB 设备寻找一些唯一的 USB 描述符。

需要帮助。

4

1 回答 1

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);
        }
    }
}
于 2017-03-29T08:12:33.897 回答