【发布时间】:2013-02-20 12:37:47
【问题描述】:
我正在寻找一种在 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] 名称:usbdev1.17 BusNumber:1 DeviceAddress:17 Length:18 DescriptorType:Device BcdUsb:0x0200 类:PerInterface 子类:0x00 协议:0x00 MaxPacketSize0:64 VendorID:0x059F ProductID:0x1014 BcdDevice:0x0000 制造商字符串索引:1 产品字符串索引:2 序列字符串索引:3 配置计数:1
[按任意键退出][DeviceType:DeviceInterface] [EventType:DeviceRemoveComplete] 名称:usbdev1.17 BusNumber:1 设备地址:17 长度:18 描述符类型:设备 BcdUsb:0x0200 类:PerInterface 子类:0x00 协议:0x00 MaxPacketSize0:64 VendorID:0x059F ProductID:0x1014 BcdDevice:0x0000 制造商字符串索引:1 产品字符串索引:2 序列字符串索引:3 配置计数:1
我的问题是如何获取附加或移除设备的路径,或者如何将 libusb 返回的信息与实际设备路径绑定?
【问题讨论】:
-
请注意,如果连接了设备,并不意味着它已安装。
-
是的,我知道,但问题是我也无法获取设备路径