从DeviceNotifyEventArgs获取usb信

问题描述 投票:1回答:1

我找不到如何提取插入的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} Vid:0x2CE3 Pid:0x6487 SerialNumber:0000000000000000 ClassGuid:a5dcbf10-6530-11d2-901f-00c04fb951ed

我正在使用LibUsbDotNet.DeviceNotify dll

例如:插入E:\

c# usb cd
1个回答
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()返回的所有搜索者和收集品都需要处理。

© www.soinside.com 2019 - 2024. All rights reserved.