Usb.Events
11.0.0.1
See the version list below for details.
dotnet add package Usb.Events --version 11.0.0.1
NuGet\Install-Package Usb.Events -Version 11.0.0.1
<PackageReference Include="Usb.Events" Version="11.0.0.1" />
<PackageVersion Include="Usb.Events" Version="11.0.0.1" />
<PackageReference Include="Usb.Events" />
paket add Usb.Events --version 11.0.0.1
#r "nuget: Usb.Events, 11.0.0.1"
#:package Usb.Events@11.0.0.1
#addin nuget:?package=Usb.Events&version=11.0.0.1
#tool nuget:?package=Usb.Events&version=11.0.0.1
Usb.Events
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.
How to use:
Include NuGet package from https://www.nuget.org/packages/Usb.Events
<ItemGroup> <PackageReference Include="Usb.Events" Version="11.0.0.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(); } }
Constructor parameters:
UsbEventWatcher(
bool startImmediately = true,
bool addAlreadyPresentDevicesToList = false,
bool usePnPEntity = false,
bool includeTTY = false)
- Set
startImmediatelytofalseif you don't want to start immediately, then callStart(). - Set
addAlreadyPresentDevicesToListtotrueto include already present devices inUsbDeviceList. - Set
usePnPEntitytotrueto queryWin32_PnPEntityinstead ofWin32_USBControllerDevicein Windows. - Set
includeTTYtotrueto monitor theTTYsubsystem in Linux (besides theUSBsubsystem).
Using Win32_PnPEntity vs Win32_USBControllerDevice
Win32_PnPEntity- PRO: works for all devices
- CON: is CPU intensive
- CON: uses only 2 methods to find
MountedDirectoryPathfor storage devices (this should still work for most devices)
Win32_USBControllerDevice- PRO: uses 3 methods to find
MountedDirectoryPathfor storage devices - PRO: in not CPU intensive
- CON: for some devices it can stop reporting
UsbDeviceAddedevent after the device is added and removed a few times
- PRO: uses 3 methods to find
Using Win32_USBControllerDevice is usually the better option.
Example:
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.
How to build:
Usb.Events.csproj uses gcc to build UsbEventWatcher.Mac.dylib from UsbEventWatcher.Mac.c when run on macOS and to build UsbEventWatcher.Linux.so from UsbEventWatcher.Linux.c when run on Linux.
On Debian/Ubuntu based Linux distros you need to install:
gcc with:
sudo apt-get install build-essential
32-bit and 64-bit udev with:
sudo apt-get install libudev-dev:i386 libudev-dev:amd64
support for compiling 32-bit on 64-bit Linux:
sudo apt-get install gcc-multilib
Usb.Events.dll expects to find UsbEventWatcher.Linux.so and UsbEventWatcher.Mac.dylib in the working directory when it runs, so make sure to build the project on Linux and Mac before building the NuGet package on Windows.
To build 32-bit and 64-bit ARM versions of UsbEventWatcher.Linux.so for Linux, you need to install Docker.
TO DO:
- Automatically mount USB drive on
UsbDeviceAddedevent in Linux - Automatically mount USB drive on
UsbDeviceAddedevent in macOS
Version history:
- 11.0.0.1
- Added 32-bit and 64-bit ARM support in Linux
- 11.0.0.0
- Added 32-bit support in Linux
- 10.1.1.1:
- Fixed
Dispose()to exit native monitor loop in macOS
- Fixed
- 10.1.1.0:
- Fixed
Dispose()to exit native monitor loop in Linux - Added
bool usePnPEntityto useWin32_PnPEntityin Windows
- Fixed
- 10.1.0.1:
- Added
bool addAlreadyPresentDevicesToListin Windows
- Added
- 10.1.0.0:
- Updated
System.Managementpackage reference from4.7.0to7.0.0
- Updated
- 10.0.1.1:
- Added
bool startImmediately = truetoUsbEventWatcherconstructor - Added
void Start(bool includeTTY = false)toIUsbEventWatcher
- Added
- 10.0.1.0:
- Added
bool includeTTY = falsetoUsbEventWatcherconstructor - Fixed a
EnumerateDevicesbug 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
NullReferenceExceptionin Linux and macOS - by @thomOrbelius
- Fixed a
- 1.1.1.1:
- Fixed a bug in Windows where
MountedDirectoryPathwasn'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
TTYsubsystem 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:
MountedDirectoryPathIsMountedIsEjected
- Breaking changes:
DevicePathrenamed toDeviceSystemPathUsbDriveInsertedrenamed toUsbDriveMountedUsbDriveRemovedrenamed toUsbDriveEjectedUsbDeviceInsertedrenamed 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
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Management (>= 7.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Usb.Events:
| Package | Downloads |
|---|---|
|
Ahsoka.Core
Package Description |
|
|
xyxandwxx.Android
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 11.1.1.1 | 543 | 11/30/2025 | |
| 11.1.1 | 14,906 | 2/8/2025 | |
| 11.1.0.1 | 47,996 | 1/5/2024 | |
| 11.1.0 | 2,140 | 11/30/2023 | |
| 11.0.1.1 | 528 | 11/17/2023 | |
| 11.0.1 | 8,588 | 7/29/2023 | |
| 11.0.0.1 | 571 | 7/21/2023 | |
| 11.0.0 | 466 | 7/16/2023 | |
| 10.1.1.1 | 957 | 6/4/2023 | |
| 10.1.1 | 340 | 5/31/2023 | |
| 10.1.0.1 | 1,091 | 4/21/2023 | |
| 10.1.0 | 479 | 4/15/2023 | |
| 10.0.1.1 | 14,641 | 11/9/2021 | |
| 10.0.1 | 656 | 11/1/2021 | |
| 10.0.0.1 | 537 | 10/31/2021 | |
| 10.0.0 | 875 | 7/2/2021 | |
| 1.1.1.1 | 9,443 | 2/13/2021 | |
| 1.1.1 | 610 | 2/1/2021 | |
| 1.1.0.1 | 1,031 | 9/19/2020 | |
| 1.1.0 | 3,957 | 8/1/2020 | |
| 1.0.1.1 | 1,004 | 7/10/2020 | |
| 1.0.1 | 2,449 | 7/4/2020 | |
| 1.0.0.1 | 697 | 7/7/2020 | |
| 1.0.0 | 3,153 | 4/28/2020 |