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



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

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

那么我如何在每次插入PC时或PC因任何原因重新启动PC后找出2个设备。

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

需要这方面的帮助。

以下代码可能有助于访问 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 + @"rootcimv2");
            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);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新