【问题标题】:Get usb letter from DeviceNotifyEventArgs从 DeviceNotifyEventArgs 获取 USB 信函
【发布时间】:2019-08-22 18:08:46
【问题描述】:

我找不到如何提取插入的 USB 字母。

我有监听插入的 USB 的事件,但我需要插入的字母,因为我有多个 USB 端口。

void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
{
  MessageBox.Show(e.ToString());
}

打印:

[DeviceType:DeviceInterface] [EventType:DeviceArrival] FullName:USB#VID_2CE3&PID_6487#0000000000000000#{a5dcbf10-6530-11d2-901f-00c04fb951ed} 视频:0x2CE3 PID:0x6487 序列号:0000000000000000 ClassGuid:a5dcbf10-6530-11d2-901f-00c04fb951ed

我正在使用LibUsbDotNet.DeviceNotify dll

例如:E:\ 插入

【问题讨论】:

  • 什么是usb letter?你是说U盘的盘符吗?
  • @Sinatr 那不一样,因为我想从事件参数中获取字母
  • 此事件是 USB 设备的通用事件,而不是 USB 驱动器。它没有为您提供此类信息。但是您可以使用一些属性(VID+PID?序列号?classguid?)使用 WMI 查找该设备并使用 this answer 获取其字母。
  • @Sinatr 如果可以,请发布代码片段作为答案

标签: c# usb cd


【解决方案1】:

由于DeviceNotifyEventArgs 包含序列号,您可以使用它在WMI 的帮助下查找设备号。

这里是this answer的改编版:

// enumerate usb drives
foreach (var drive in new ManagementObjectSearcher("select * from Win32_DiskDrive where InterfaceType='USB'").Get())
    // find driver with known serial number
    if ((string)drive.Properties["SerialNumber"].Value == serialNumber)
        // associate partitions with drive
        foreach (var partition in new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" + drive["DeviceID"] + "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition").Get())
            // associate logical disk with partition
            foreach (var disk in new ManagementObjectSearcher("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" + partition["DeviceID"] + "'} WHERE AssocClass = Win32_LogicalDiskToPartition").Get())
                Console.WriteLine("Disk=" + disk["Name"]);

确保将所有内容包装到 using 中:Get() 返回的所有搜索器和集合都需要处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 2021-10-13
    相关资源
    最近更新 更多