在 .NET 中获取连接的 USB 设备的型号名称
Getting the model name of a connected USB device in .NET
在我的 WinForms 应用程序中,我想获取已连接 USB 对象的列表,然后获取这些已连接 USB 设备的型号 属性,因为我需要能够在用户连接时告知他们某个 USB 设备与否。
任何人都可以通过 WMI 或其他方式分享一种方法,以获取已连接设备的列表并获取这些设备的型号 属性 吗?我改编了 this question on using WMI to get device information. The code below works fine but the parameters that are obtained ("DeviceID", "PNPDeviceID" etc.) aren't useful at all when it comes to identifying the devices in human understandable terms and I don't know how to get the Model property that is shown in Devices and Printers as seen here.
中的一些代码
public static List<USBDeviceInfo> GetUSBDevices(){
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From
Win32_USBControllerDevice")) collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")));
}
collection.Dispose();
return devices;
}
使用 class
public class USBDeviceInfo {
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
}
}
我想做这个 post 以防我缺少针对我的特定问题的更简单的解决方案。我目前也在尝试对 USB View ala this question 进行逆向工程,因为 USB view 正在以某种方式获取我需要的信息。
郑重声明,我在 Visual Studio 2015 年使用 C#。
我能够通过解决方法实现我需要的顶级功能;我确定了返回的 DeviceID 字符串的一部分,它是我的应用程序所需的 RFID 扫描仪品牌所独有的。
我分析了返回的 deviceID 字符串并检查了该唯一值是否存在,并相应地更新了我的应用程序以反映这一点。
在我的 WinForms 应用程序中,我想获取已连接 USB 对象的列表,然后获取这些已连接 USB 设备的型号 属性,因为我需要能够在用户连接时告知他们某个 USB 设备与否。
任何人都可以通过 WMI 或其他方式分享一种方法,以获取已连接设备的列表并获取这些设备的型号 属性 吗?我改编了 this question on using WMI to get device information. The code below works fine but the parameters that are obtained ("DeviceID", "PNPDeviceID" etc.) aren't useful at all when it comes to identifying the devices in human understandable terms and I don't know how to get the Model property that is shown in Devices and Printers as seen here.
中的一些代码public static List<USBDeviceInfo> GetUSBDevices(){
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From
Win32_USBControllerDevice")) collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description")));
}
collection.Dispose();
return devices;
}
使用 class
public class USBDeviceInfo {
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID { get; private set; }
public string PnpDeviceID { get; private set; }
public string Description { get; private set; }
}
}
我想做这个 post 以防我缺少针对我的特定问题的更简单的解决方案。我目前也在尝试对 USB View ala this question 进行逆向工程,因为 USB view 正在以某种方式获取我需要的信息。
郑重声明,我在 Visual Studio 2015 年使用 C#。
我能够通过解决方法实现我需要的顶级功能;我确定了返回的 DeviceID 字符串的一部分,它是我的应用程序所需的 RFID 扫描仪品牌所独有的。
我分析了返回的 deviceID 字符串并检查了该唯一值是否存在,并相应地更新了我的应用程序以反映这一点。