我正在寻找一种在 C# 中获取 USB 到达和移除事件的跨平台方式,我发现了“LibUsbDotNet C# USB 库”(http://sourceforge.net/projects/libusbdotnet/?source=navbar)。
它可以正常工作,但在 Linux 中,我似乎无法获取设备挂载点(路径)。在 Linux 中,它使用“libusb”库,该库没有获取设备路径的方法。
这是一个检测设备事件的简单代码示例:
internal class DeviceNotification
{
public static IDeviceNotifier UsbDeviceNotifier = DeviceNotifier.OpenDeviceNotifier();
private static void Main(string[] args)
{
// Hook the device notifier event
UsbDeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent;
// Exit on and key pressed.
Console.Clear();
Console.WriteLine();
Console.WriteLine("Waiting for system level device events..");
Console.Write("[Press any key to exit]");
while (!Console.KeyAvailable)
Application.DoEvents();
UsbDeviceNotifier.Enabled = false; // Disable the device notifier
// Unhook the device notifier event
UsbDeviceNotifier.OnDeviceNotify -= OnDeviceNotifyEvent;
}
private static void OnDeviceNotifyEvent(object sender, DeviceNotifyEventArgs e)
{
// A Device system-level event has occured
Console.SetCursorPosition(0,Console.CursorTop);
Console.WriteLine(e.ToString()); // Dump the event info to output.
Console.WriteLine();
Console.Write("[Press any key to exit]");
}
}
这是输出示例:
[DeviceType:DeviceInterface] [EventType:DeviceArrival] Name:usbdev1.17 BusNumber:1 DeviceAddress:17 Length:18 DescriptorType:Device BcdUsb:0x0200 Class:PerInterface SubClass:0x00 Protocol:0x00 MaxPacketSize0:64 VendorID:0x059F ProductID:0x1014 BcdDevice: 0x0000 制造商字符串索引:1 产品字符串索引:2 序列字符串索引:3 配置计数:1
[按任意键退出][DeviceType:DeviceInterface] [EventType:DeviceRemoveComplete] Name:usbdev1.17 BusNumber:1 DeviceAddress:17 Length:18 DescriptorType:Device BcdUsb:0x0200 Class:PerInterface SubClass:0x00 Protocol:0x00 MaxPacketSize0:64 VendorID :0x059F ProductID:0x1014 BcdDevice:0x0000 ManufacturerStringIndex:1 ProductStringIndex:2 SerialStringIndex:3 ConfigurationCount:1
我的问题是如何获取附加或删除设备的路径,或者如何将 libusb 返回的信息与实际设备路径绑定?