[英]Get usb letter from DeviceNotifyEventArgs
I can't find how to extract the inserted usb letter. 我找不到如何提取插入的USB字母。
I have event that listen to usb that inserted, but I need the inserted letter, because I have mulpile usb ports. 我有监听USB插入的事件,但是我需要插入的字母,因为我有多个USB端口。
void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
{
MessageBox.Show(e.ToString());
}
Prints: 印刷品:
[DeviceType:DeviceInterface] [EventType:DeviceArrival] FullName:USB#VID_2CE3&PID_6487#0000000000000000#{a5dcbf10-6530-11d2-901f-00c04fb951ed} Vid:0x2CE3 Pid:0x6487 SerialNumber:0000000000000000 ClassGuid:a5dcbf10-6530-11d2-901f-00c04fb951ed
[DeviceType:DeviceInterface] [EventType:DeviceArrival]完整名称:USB#VID_2CE3&PID_6487#0000000000000000#{a5dcbf10-6530-11d2-901f-00c04fb951ed} Vid:0x2CE3 Pid:0x6487序列号:0000000000000000 ClassGuid:a5dcbff10-6530-11d2-901
I'm using LibUsbDotNet.DeviceNotify dll 我正在使用LibUsbDotNet.DeviceNotify dll
For example: E:\\
inserted 例如:
E:\\
插入
Since DeviceNotifyEventArgs
contains serial number you can use it to find device letter with help of WMI. 由于
DeviceNotifyEventArgs
包含序列号,因此您可以在WMI的帮助下使用它来查找设备字母。
Here is adapted version of 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"]);
Make sure to wrap everything into using
: all searchers and collection returned by Get()
needs to be disposed. 确保将所有内容包装成
using
:需要处理Get()
返回的所有搜索器和集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.