我正在尝试获取已连接 USB 设备的名称,例如手机或 USB 记忆棒。

在获得这些方法之前我浏览了 stackoverflow,但我找不到正确的属性。
static List<USBDeviceInfo> GetUSBDevices()
{
List<USBDeviceInfo> devices = new List<USBDeviceInfo>();
ManagementObjectCollection collection;
using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach (var device in collection)
{
devices.Add(new USBDeviceInfo(
(string)device.GetPropertyValue("DeviceID"),
(string)device.GetPropertyValue("PNPDeviceID"),
(string)device.GetPropertyValue("Description"),
(string)device.GetPropertyValue("Name"),
(string)device.GetPropertyValue("Caption")
));
}
collection.Dispose();
return devices;
}
Run Code Online (Sandbox Code Playgroud)
USB设备信息类:
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description, string name, string caption)
{
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
this.Name = name;
this.Caption = caption; …Run Code Online (Sandbox Code Playgroud)