Pea*_*Gen 6 .net c# usb visual-studio-2010
我需要遍历连接到计算机的端口,并找到特定的设备.请看下面的图片:

您可以看到有4个设备名称具有相同的供应商和产品ID.但是,我需要找到第一个的端口,它以蓝色突出显示.看起来他们唯一的区别就是朋友的名字(描述是朋友的名字).
在C#.net中实现这一目标的最简单方法是什么?我在'qt'中完成了这个,我需要知道如何使用vs 2010 professional在C#,.net framework 4中完成此操作.我已经经历了这样的问题了这样,但你可以看到,他们是我的情况没有帮助.
如果你使用libusbdotnet,你应该可以做这样的事情:
public static void RetrieveUSBDevices(int vid, int pid)
{
var usbFinder = new UsbDeviceFinder(vid, pid);
var usbDevices = new UsbRegDeviceList();
usbDevices = usbDevices.FindAll(usbFinder);
}
Run Code Online (Sandbox Code Playgroud)
然后,您应该能够迭代usbDevices并检查正确的FullName.虽然这是理论上的,但我没有对此进行测试.
更新:试过,它工作正常 - 问题是什么?为什么因为你自己的无能而退缩?
这也有效:
private static void Method()
{
var list = GetMyUSBDevices();
//Iterate list here and use Description to find exact device
}
private static List<UsbDevice> GetMyUSBDevices()
{
var vid = 32903;
var pid = 36;
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();
var usbDevice =
(from ManagementBaseObject device in collection
select new UsbDevice(
(string) device.GetPropertyValue("DeviceID"),
(string) device.GetPropertyValue("Description"))).ToList();
var devices = new List<UsbDevice>();
foreach (var device in collection)
{
devices.Add(new UsbDevice(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("Description")
));
}
collection.Dispose();
return (from d in devices where d.DeviceId.Contains("VID_") && d.DeviceId.Contains("PID_") && d.PID.Equals(pid) && d.VID.Equals(vid) select d).ToList();
}
public class UsbDevice
{
public UsbDevice(string deviceId, string description)
{
DeviceId = deviceId;
Description = description;
}
public string DeviceId { get; private set; }
public string Description { get; private set; }
public int VID
{
get { return int.Parse(GetIdentifierPart("VID_"), System.Globalization.NumberStyles.HexNumber); }
}
public int PID
{
get { return int.Parse(GetIdentifierPart("PID_"), System.Globalization.NumberStyles.HexNumber); }
}
private string GetIdentifierPart(string identifier)
{
var vidIndex = DeviceId.IndexOf(identifier, StringComparison.Ordinal);
var startingAtVid = DeviceId.Substring(vidIndex + 4);
return startingAtVid.Substring(0, 4);
}
}
Run Code Online (Sandbox Code Playgroud)