Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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.

License

NotificationsYou must be signed in to change notification settings

Jinjinov/Usb.Events

Repository files navigation

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:

  1. Include NuGet package fromhttps://www.nuget.org/packages/Usb.Events

     <ItemGroup>     <PackageReference Include="Usb.Events" Version="11.1.1.0" /> </ItemGroup>
  2. 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)
  • SetstartImmediately tofalse if you don't want to start immediately, then callStart().
  • SetaddAlreadyPresentDevicesToList totrue to include already present devices inUsbDeviceList.
  • SetusePnPEntity totrue to queryWin32_PnPEntity instead ofWin32_USBControllerDevice in Windows.
  • SetincludeTTY totrue to monitor theTTY subsystem in Linux (besides theUSB subsystem).

UsingWin32_PnPEntity vsWin32_USBControllerDevice

  • Win32_PnPEntity
    • PRO: works for all devices
    • CON: is CPU intensive
    • CON: uses only 2 methods to findMountedDirectoryPath for storage devices (this should still work for most devices)
  • Win32_USBControllerDevice
    • PRO: uses 3 methods to findMountedDirectoryPath for storage devices
    • PRO: in not CPU intensive
    • CON: for some devices it can stop reportingUsbDeviceAdded event after the device is added and removed a few times

UsingWin32_USBControllerDevice is usually the better option.

Example:

Usb.Events.Example demonstrates how to use WindowsSetupAPI.dll functionsSetupDiGetClassDevs,SetupDiEnumDeviceInfo andSetupDiGetDeviceProperty together withDEVPKEY_Device_DeviceDesc,DEVPKEY_Device_BusReportedDeviceDesc andDEVPKEY_Device_FriendlyName to get "Device description", "Bus reported device description" and "Friendly name" of theUsb.Events.UsbDevice reported by theUsb.Events.IUsbEventWatcher.UsbDeviceAdded event.

How to build:

Usb.Events.csproj usesgcc to buildUsbEventWatcher.Mac.dylib fromUsbEventWatcher.Mac.c when run on macOS and to buildUsbEventWatcher.Linux.so fromUsbEventWatcher.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 findUsbEventWatcher.Linux.so andUsbEventWatcher.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 ofUsbEventWatcher.Linux.so on Windows, you need to install Docker.

TO DO:

  • Automatically mount USB drive onUsbDeviceAdded event in Linux
  • Automatically mount USB drive onUsbDeviceAdded event in macOS

Version history:

  • 11.1.1.0 (2025-01-29):
  • 11.1.0.1 (2024-01-05):
    • Added XML documentation
  • 11.1.0.0 (2023-11-30):
    • AddedUsbEvents.snk to sign the assembly with a strong name key
  • 11.0.1.1 (2023-11-17):
    • Added 64-bit ARM support in macOS - thanks to@slater1
  • 11.0.1.0 (2023-07-29):
    • FixedInvalidOperationException in Linux and macOS - by@Frankh67
  • 11.0.0.1 (2023-07-21):
    • Added 32-bit and 64-bit ARM support in Linux
  • 11.0.0.0 (2023-07-16):
    • Added 32-bit support in Linux
  • 10.1.1.1 (2023-06-04):
    • FixedDispose() to exit native monitor loop in macOS
  • 10.1.1.0 (2023-06-01):
    • FixedDispose() to exit native monitor loop in Linux
    • Addedbool usePnPEntity to useWin32_PnPEntity in Windows
  • 10.1.0.1 (2023-04-21):
    • Addedbool addAlreadyPresentDevicesToList in Windows
  • 10.1.0.0 (2023-04-15):
    • UpdatedSystem.Management package reference from4.7.0 to7.0.0
  • 10.0.1.1 (2021-11-09):
    • Addedbool startImmediately = true toUsbEventWatcher constructor
    • Addedvoid Start(bool includeTTY = false) toIUsbEventWatcher
  • 10.0.1.0 (2021-11-01):
    • Addedbool includeTTY = false toUsbEventWatcher constructor
    • Fixed aEnumerateDevices bug in Linux - thanks to@d79ima
  • 10.0.0.1 (2021-10-31):
    • Fixed a false "device added" events bug in Linux - thanks to@d79ima
  • 10.0.0.0 (2021-07-02):
    • Fixed aNullReferenceException in Linux and macOS - by@thomOrbelius
  • 1.1.1.1 (2021-02-13):
    • Fixed a bug in Windows whereMountedDirectoryPath wasn't set for a disk drive - thanks to@cksoft0807
  • 1.1.1.0 (2021-02-01):
    • Fixed a memory leak in Linux functionGetLinuxMountPoint - by@maskimthedog
    • Fixed a bug in Linux where after instantiatingUsbEventWatcher, the list of devices was empty - by@maskimthedog
    • Added monitoring ofTTY subsystem in Linux - by@maskimthedog
    • Fixed a bug in Linux where monitoring would stop upon error - by@maskimthedog
  • 1.1.0.1 (2020-09-19):
    • Fixed a bug
  • 1.1.0.0 (2020-08-01):
    • Added:
      • MountedDirectoryPath
      • IsMounted
      • IsEjected
    • Breaking changes:
      • DevicePath renamed toDeviceSystemPath
      • UsbDriveInserted renamed toUsbDriveMounted
      • UsbDriveRemoved renamed toUsbDriveEjected
      • UsbDeviceInserted renamed toUsbDeviceAdded
  • 1.0.1.1 (2020-07-10):
    • Fixed a bug
  • 1.0.1.0 (2020-07-04):
    • Events for all USB devices
  • 1.0.0.1 (2020-07-07):
    • Fixed a bug
  • 1.0.0.0 (2020-04-28):
    • Events for USB drives and USB storage devices

Supported platforms:

versionlinux-armlinux-arm64linux-x64linux-x86osx-arm64osx-x64
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

About

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.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp