Subscribe to the Inserted and Removed events to be notified when a USB drive is plugged in or unplugged, or when a USB device is connected or disconnected. Usb.Events is a .NET Standard 2.0 library and uses WMI on Windows, libudev on Linux and IOKit on macOS.
-
Include NuGet package from https://www.nuget.org/packages/Usb.Events
<ItemGroup> <PackageReference Include="Usb.Events" Version="10.0.1.1" /> </ItemGroup>
-
Subscribe to events:
using Usb.Events; class Program { static void Main(string[] _) { using IUsbEventWatcher usbEventWatcher = new UsbEventWatcher(); usbEventWatcher.UsbDeviceRemoved += (_, device) => Console.WriteLine("Removed:" + Environment.NewLine + device + Environment.NewLine); usbEventWatcher.UsbDeviceAdded += (_, device) => Console.WriteLine("Added:" + Environment.NewLine + device + Environment.NewLine); usbEventWatcher.UsbDriveEjected += (_, path) => Console.WriteLine("Ejected:" + Environment.NewLine + path + Environment.NewLine); usbEventWatcher.UsbDriveMounted += (_, path) => { Console.WriteLine("Mounted:" + Environment.NewLine + path + Environment.NewLine); foreach (string entry in Directory.GetFileSystemEntries(path)) Console.WriteLine(entry); Console.WriteLine(); }; Console.ReadLine(); } }
UsbEventWatcher(bool startImmediately = true, bool includeTTY = false)
- Set
startImmediately
tofalse
if you don't want to start immediately.
Then call theStart(bool includeTTY = false)
method. - Set
includeTTY
totrue
if you want to monitor theTTY
subsystem in Linux (besides theUSB
subsystem).
Usb.Events.Example
demonstrates how to use Windows SetupAPI.dll
functions SetupDiGetClassDevs, SetupDiEnumDeviceInfo and SetupDiGetDeviceProperty together with DEVPKEY_Device_DeviceDesc, DEVPKEY_Device_BusReportedDeviceDesc and DEVPKEY_Device_FriendlyName to get "Device description", "Bus reported device description" and "Friendly name" of the Usb.Events.UsbDevice
reported by the Usb.Events.IUsbEventWatcher.UsbDeviceAdded
event.
- Automatically mount USB drive on
UsbDeviceAdded
event in Linux - Automatically mount USB drive on
UsbDeviceAdded
event in macOS
- 10.0.1.1:
- Added
bool startImmediately = true
toUsbEventWatcher
constructor - Added
void Start(bool includeTTY = false)
toIUsbEventWatcher
- Added
- 10.0.1.0:
- Added
bool includeTTY = false
toUsbEventWatcher
constructor - Fixed a
EnumerateDevices
bug in Linux - thanks to @d79ima
- Added
- 10.0.0.1:
- Fixed a false "device added" events bug in Linux - thanks to @d79ima
- 10.0.0.0:
- Fixed a
NullReferenceException
in Linux and macOS - by @thomOrbelius
- Fixed a
- 1.1.1.1:
- Fixed a bug in Windows where
MountedDirectoryPath
wasn't set for a disk drive - thanks to @cksoft0807
- Fixed a bug in Windows where
- 1.1.1.0:
- Fixed a memory leak in Linux function
GetLinuxMountPoint
- by @maskimthedog - Fixed a bug in Linux where after instantiating
UsbEventWatcher
, the list of devices was empty - by @maskimthedog - Added monitoring of
TTY
subsystem in Linux - by @maskimthedog - Fixed a bug in Linux where monitoring would stop upon error - by @maskimthedog
- Fixed a memory leak in Linux function
- 1.1.0.1:
- Fixed a bug
- 1.1.0.0:
- Added:
MountedDirectoryPath
IsMounted
IsEjected
- Breaking changes:
DevicePath
renamed toDeviceSystemPath
UsbDriveInserted
renamed toUsbDriveMounted
UsbDriveRemoved
renamed toUsbDriveEjected
UsbDeviceInserted
renamed toUsbDeviceAdded
- Added:
- 1.0.1.1:
- Fixed a bug
- 1.0.1.0:
- Events for all USB devices
- 1.0.0.1:
- Fixed a bug
- 1.0.0.0:
- Events for USB drives and USB storage devices