我需要遍历连接到计算机的端口,并找到特定的设备.请看下面的图片:
您可以看到有4个设备名称具有相同的供应商和产品ID.但是,我需要找到第一个的端口,它以蓝色突出显示.看起来他们唯一的区别就是朋友的名字(描述是朋友的名字).
在C#.net中实现这一目标的最简单方法是什么?我在’qt’中完成了这个,我需要知道如何使用vs 2010 professional在C#,.net framework 4中完成此操作.我已经经历了像this这样的问题,但正如你所看到的,他们对我的情况毫无帮助.
解决方法
如果您使用
libusbdotnet,您应该能够做到这样的事情:
public static void RetrieveUSBDevices(int vid,int pid) { var usbFinder = new UsbDeviceFinder(vid,pid); var usbDevices = new UsbRegDeviceList(); usbDevices = usbDevices.FindAll(usbFinder); }
然后你应该能够迭代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); } }
原文地址:https://www.jb51.cc/csharp/92425.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。