![]() |
VOOZH | about |
dotnet add package Usb.Events --version 11.1.1.1
NuGet\Install-Package Usb.Events -Version 11.1.1.1
<PackageReference Include="Usb.Events" Version="11.1.1.1" />
<PackageVersion Include="Usb.Events" Version="11.1.1.1" />Directory.Packages.props
<PackageReference Include="Usb.Events" />Project file
paket add Usb.Events --version 11.1.1.1
#r "nuget: Usb.Events, 11.1.1.1"
#:package Usb.Events@11.1.1.1
#addin nuget:?package=Usb.Events&version=11.1.1.1Install as a Cake Addin
#tool nuget:?package=Usb.Events&version=11.1.1.1Install as a Cake Tool
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="11.1.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 addAlreadyPresentDevicesToList = false,
bool usePnPEntity = false,
bool includeTTY = false)
startImmediately to false if you don't want to start immediately, then call Start().addAlreadyPresentDevicesToList to true to include already present devices in UsbDeviceList.usePnPEntity to true to query Win32_PnPEntity instead of Win32_USBControllerDevice in Windows.includeTTY to true to monitor the TTY subsystem in Linux (besides the USB subsystem).Win32_PnPEntity vs Win32_USBControllerDeviceWin32_PnPEntity
MountedDirectoryPath for storage devices (this should still work for most devices)Win32_USBControllerDevice
MountedDirectoryPath for storage devicesUsbDeviceAdded event after the device is added and removed a few timesUsing Win32_USBControllerDevice is usually the better option.
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.
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.
32-bit Intel macOS:
gcc -shared -m32 ./Mac/UsbEventWatcher.Mac.c -o ./x86/Release/UsbEventWatcher.Mac.dylib -framework CoreFoundation -framework DiskArbitration -framework IOKit
32-bit Intel Linux:
gcc -shared -m32 ./Linux/UsbEventWatcher.Linux.c -o ./x86/Release/UsbEventWatcher.Linux.so -ludev -fPIC
64-bit Intel macOS:
gcc -shared -m64 ./Mac/UsbEventWatcher.Mac.c -o ./x64/Release/UsbEventWatcher.Mac.dylib -framework CoreFoundation -framework DiskArbitration -framework IOKit
64-bit Intel Linux:
gcc -shared -m64 ./Linux/UsbEventWatcher.Linux.c -o ./x64/Release/UsbEventWatcher.Linux.so -ludev -fPIC
32-bit ARM macOS:
gcc -shared -march=armv7-a+fp ./Mac/UsbEventWatcher.Mac.c -o ./arm/Release/UsbEventWatcher.Mac.dylib -framework CoreFoundation -framework DiskArbitration -framework IOKit
32-bit ARM Linux:
gcc -shared -march=armv7-a+fp ./Linux/UsbEventWatcher.Linux.c -o ./arm/Release/UsbEventWatcher.Linux.so -ludev -fPIC
64-bit ARM macOS:
gcc -shared -march=armv8-a ./Mac/UsbEventWatcher.Mac.c -o ./arm64/Release/UsbEventWatcher.Mac.dylib -framework CoreFoundation -framework DiskArbitration -framework IOKit
64-bit ARM Linux:
gcc -shared -march=armv8-a ./Linux/UsbEventWatcher.Linux.c -o ./arm64/Release/UsbEventWatcher.Linux.so -ludev -fPIC
To build 32-bit and 64-bit ARM versions of UsbEventWatcher.Linux.so on Windows, you need to install Docker.
Due to changes in macOS Gatekeeper that were introduced sometime between May 28, 2025 and July 15, 2025, simply building and running the code on macOS no longer works by default.
macOS may block the included .dylib files, reporting that it "couldn’t verify the software for malicious content".
To use the code successfully, the .dylib must be signed with a Developer ID Application certificate.
UsbDeviceAdded event in LinuxUsbDeviceAdded event in macOSGetChild in Linux - thanks to @M0ns1gn0rUsbEvents.snk to sign the assembly with a strong name keyInvalidOperationException in Linux and macOS - by @Frankh67Dispose() to exit native monitor loop in macOSDispose() to exit native monitor loop in Linuxbool usePnPEntity to use Win32_PnPEntity in Windowsbool addAlreadyPresentDevicesToList in WindowsSystem.Management package reference from 4.7.0 to 7.0.0bool startImmediately = true to UsbEventWatcher constructorvoid Start(bool includeTTY = false) to IUsbEventWatcherbool includeTTY = false to UsbEventWatcher constructorEnumerateDevices bug in Linux - thanks to @d79imaNullReferenceException in Linux and macOS - by @thomOrbeliusMountedDirectoryPath wasn't set for a disk drive - thanks to @cksoft0807GetLinuxMountPoint - by @maskimthedogUsbEventWatcher, the list of devices was empty - by @maskimthedogTTY subsystem in Linux - by @maskimthedogMountedDirectoryPathIsMountedIsEjectedDevicePath renamed to DeviceSystemPathUsbDriveInserted renamed to UsbDriveMountedUsbDriveRemoved renamed to UsbDriveEjectedUsbDeviceInserted renamed to UsbDeviceAdded| version | linux-arm | linux-arm64 | linux-x64 | linux-x86 | osx-arm64 | osx-x64 |
|---|---|---|---|---|---|---|
| 11.1.1.1 | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
| 11.1.1.0 | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
| 11.1.0.1 | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
| 11.1.0.0 | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
| 11.0.1.1 | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
| 11.0.1.0 | ✔ | ✔ | ✔ | ✔ | ✔ | |
| 11.0.0.1 | ✔ | ✔ | ✔ | ✔ | ✔ | |
| 11.0.0.0 | ✔ | ✔ | ✔ | ✔ | ✔ | |
| 10.1.1.1 | ✔ | ✔ | ||||
| 10.1.1.0 | ✔ | ✔ | ||||
| 10.1.0.1 | ✔ | ✔ | ||||
| 10.1.0.0 | ✔ | ✔ | ||||
| 10.0.1.1 | ✔ | ✔ | ||||
| 10.0.1.0 | ✔ | ✔ | ||||
| 10.0.0.1 | ✔ | ✔ | ||||
| 10.0.0.0 | ✔ | ✔ | ||||
| 1.1.1.1 | ✔ | ✔ | ||||
| 1.1.1.0 | ✔ | ✔ | ||||
| 1.1.0.1 | ✔ | ✔ | ||||
| 1.1.0.0 | ✔ | ✔ | ||||
| 1.0.1.1 | ✔ | ✔ | ||||
| 1.0.1.0 | ✔ | ✔ | ||||
| 1.0.0.1 | ✔ | ✔ | ||||
| 1.0.0.0 | ✔ | ✔ |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 2 NuGet packages that depend on Usb.Events:
| Package | Downloads |
|---|---|
|
Ahsoka.Core
Package Description |
|
|
xyxandwxx.Android
Package Description |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 11.1.1.1 | 13,046 | 11/30/2025 |
| 11.1.1 | 23,186 | 2/8/2025 |
| 11.1.0.1 | 51,074 | 1/5/2024 |
| 11.1.0 | 2,174 | 11/30/2023 |
| 11.0.1.1 | 574 | 11/17/2023 |
| 11.0.1 | 8,734 | 7/29/2023 |
| 11.0.0.1 | 596 | 7/21/2023 |
| 11.0.0 | 497 | 7/16/2023 |
| 10.1.1.1 | 1,030 | 6/4/2023 |
| 10.1.1 | 375 | 5/31/2023 |
| 10.1.0.1 | 1,152 | 4/21/2023 |
| 10.1.0 | 519 | 4/15/2023 |
| 10.0.1.1 | 15,577 | 11/9/2021 |
| 10.0.1 | 693 | 11/1/2021 |
| 10.0.0.1 | 565 | 10/31/2021 |
| 10.0.0 | 921 | 7/2/2021 |
| 1.1.1.1 | 10,707 | 2/13/2021 |
| 1.1.1 | 646 | 2/1/2021 |
| 1.1.0.1 | 1,062 | 9/19/2020 |
| 1.1.0 | 3,995 | 8/1/2020 |