Reputation: 51
I was looking for a cross platform way of getting usb arrival and removal events in C# and i found "LibUsbDotNet C# USB Library" (http://sourceforge.net/projects/libusbdotnet/?source=navbar).
It works as it should but in Linux it seems that i can't get device mount point (path). In Linux it uses "libusb" library which does not have a method for getting the device path.
Here is a simple code sample that detects the device events:
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]");
}
}
and here is a sample of the output:
[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 ManufacturerStringIndex:1 ProductStringIndex:2 SerialStringIndex:3 ConfigurationCount:1
[Press any key to exit][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
My question is how can I get the path of the device attached or removed, or how can I bind the info returned by libusb with an actual device path?
Upvotes: 5
Views: 4446
Reputation: 5683
You need to use UDev instead of libusb. Libusb merely tells you about what USB devices are on the system, but won't tell you anything about where they're mounted. UDev handles mounting them.
There is libudev and the documentation should be here: http://www.freedesktop.org/software/systemd/libudev/ but it appears to be down at the moment. Here's a tutorial on libudev: Tutorial: How to use libudev and SysFS in Linux
There is also a GLib based wrapper for libudev, docs here: http://ftp.osuosl.org/pub/linux/utils/kernel/hotplug/gudev/ and there appears to be a c# wrapper for libgudev.
But finally you may find using GLib's GIO easier than getting down to the udev level: Volumes and Drives API reference.
Upvotes: 2
Reputation: 1482
USB device files are usually stored in the path:
/dev/bus/usb
In that folder will be sub-directories that should match with your bus numbers above. Things will get complicated if the USB device isn't directly attached to the computer, for instance through a hub or other external device. Don't forget to convert from hex.
Upvotes: -1