小编cKa*_*Kai的帖子

如何检索 USB 设备的名称

我正在尝试获取已连接 USB 设备的名称,例如手机或 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)

c# usb-drive

4
推荐指数
1
解决办法
3143
查看次数

标签 统计

c# ×1

usb-drive ×1