我找不到如何提取插入的USB字母。
我有监听USB插入的事件,但是我需要插入的字母,因为我有多个USB端口。
void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
{
MessageBox.Show(e.ToString());
}
打印:
[DeviceType:DeviceInterface] [EventType:DeviceArrival]完整名称:USB#VID_2CE3&PID_6487#0000000000000000#{a5dcbf10-6530-11d2-901f-00c04fb951ed} 视频:0x2CE3 编号:0x6487 序列号:0000000000000000 ClassGuid:a5dcbf10-6530-11d2-901f-00c04fb951ed
我正在使用LibUsbDotNet.DeviceNotify dll
例如:插入E:\
答案 0 :(得分:0)
由于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()
返回的所有搜索器和集合都必须进行处理。