Skip to content

bakhshipoor/USBDevices

Repository files navigation

USB Devices Library

This library provides functionalities to list USB devices on Windows and handle events for device changes. It is based on C++ code and utilizes Windows IOCTL, setupapi.dll, kernel32.dll, and fmifs.dll.

Features

  • List all connected USB devices
  • Monitor USB device connection and disconnection events
  • Filter USB devices by VID, PID, and Service

Requirements

  • .Net Core 8.0
  • Windows Desktop

Supported Interfaces

  • Disk Drive
  • Disk Partition
  • Logical Disk

Event Types

  • Connected
  • Disconnected

Installation

To install this library, clone the repository and build the solution using Visual Studio.

git clone https://github.com/bakhshipoor/USBDevices.git
cd USBDevices

Usage

  1. Create a new instance of the USBDevicesList class:
public USBDevicesList USBDevicesCollection { get; set; }

USBDevicesCollection = new USBDevicesList();
  1. Enable or disable connection and disconnection events as needed:
USBDevicesCollection.ConnectedEventStatus = false;
USBDevicesCollection.DisconnectedEventStatus = true;
  1. Enable device filtering and set filters:
USBDevicesCollection.EnableFilterDevice();
USBDevicesCollection.FilterDeviceStatus = true;
USBDevicesCollection.SetDeviceToFilter("xxxx", "yyyy", "Service Name");
  1. Add event handlers for initial collection completion and device changes:
USBDevicesCollection.InitialCollectionsComplete += USBDevicesCollections_InitialCollectionsComplete;
USBDevicesCollection.DeviceChanged += USBDevicesCollections_DeviceChanged;
  1. Start monitoring USB devices:
USBDevicesCollection.Start();
  1. Access the list of USB devices:
ObservableCollection<USBDevice> devices = USBDevicesCollection.USBDevices;

Examples

This library includes two example projects:

  1. USBDevicesDemo: A simple WPF application to view properties of connected USB devices.
  2. CopyFilesToFlash: A WPF MVVM application that automatically copies files to a USB flash drive.

Screenshots

USBDevicesDemo-1 USBDevicesDemo-2 USBDevicesDemo-3 USBDevicesDemo-4 CopyFilesToFlash-1 CopyFilesToFlash-2

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please fork the repository and submit a pull request.

Acknowledgements

Thanks to all contributors and supporters.


Feel free to check the repository at USBDevices.