Managed wrappers around SetupAPI, Cfgmgr32, NewDev and DrvStore Windows APIs.
- Listen for device plugin and unplug events without depending on WinForms or WPF
- Enumerate and remove elements from the Driver Store
- Convert various notations (Symlink to Instance ID etc.)
- Get and set various unified device properties
- Enumerate devices (present and absent)
- Hot-swap device function drivers
- Add/Remove/Clear class filters
- Power-cycle USB hub ports
- Install/update drivers
- Uninstall devices
See TESTING.md for Unit/CI filters, hardware lab prerequisites, and how tests run in GitHub Actions.
dotnet test Tests/Tests.csproj -c Release --filter "Category=Unit|Category=CI"Some usage examples of the core library features are presented below. Samples assume:
using System;
using System.Linq;
using System.Threading;
using Nefarius.Utilities.DeviceManagement.Drivers;
using Nefarius.Utilities.DeviceManagement.Extensions;
using Nefarius.Utilities.DeviceManagement.PnP;The Devcon utility class offers helper methods to find devices.
var instance = 0;
// enumerate all devices that export the GUID_DEVINTERFACE_USB_DEVICE interface
while (Devcon.FindByInterfaceGuid(DeviceInterfaceIds.UsbDevice, out var path,
out var instanceId, instance++))
{
Console.WriteLine($"Path: {path}, InstanceId: {instanceId}");
var usbDevice = PnPDevice
.GetDeviceByInterfaceId(path)
.ToUsbPnPDevice();
Console.WriteLine($"Got USB device {usbDevice.InstanceId}");
}One or more instances of the DeviceNotificationListener can be used to listen for plugin and unplug events of various
devices. This class has no dependency on WinForms or WPF and works in Console Applications and Windows Services alike.
var listener = new DeviceNotificationListener();
listener.DeviceArrived += Console.WriteLine;
listener.DeviceRemoved += Console.WriteLine;
// start listening for plugins or unplugs of GUID_DEVINTERFACE_USB_DEVICE interface devices
listener.StartListen(DeviceInterfaceIds.UsbDevice);var allDriverPackages = DriverStore.ExistingDrivers.ToList();foreach (var driverPackage in allDriverPackages.Where(p => p.Contains("mydriver.inf", StringComparison.OrdinalIgnoreCase)))
{
DriverStore.RemoveDriver(driverPackage);
}// e.g. the path "\\?\HID#VID_045E&PID_028E&IG_00#3&31f0e99d&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"
// gets translated to instanceId "HID\VID_045E&PID_028E&IG_00\3&31f0e99d&0&0000"
var instanceId = PnPDevice.GetInstanceIdFromInterfaceId(path);// example path: "\\?\HID#VID_046D&PID_C33F&MI_01&COL02#B&31580538&0&0001#{4D1E55B2-F16F-11CF-88CB-001111000030}"
PnPDevice device = PnPDevice.GetDeviceByInterfaceId(path);string service = "HidHide";
DeviceClassFilters.AddUpper(DeviceClassIds.XnaComposite, service);const string instanceId = @"USB\VID_054C&PID_0CE6&MI_03\9&DC32669&3&0003";
PnPDevice device = PnPDevice.GetDeviceByInstanceId(instanceId);
// required for the next call to succeed
device.InstallNullDriver();
Thread.Sleep(1000);
// accepts any INF already present in C:\Windows\INF directory
device.InstallCustomDriver("winusb.inf");const string instanceId = @"USB\VID_054C&PID_0CE6&MI_03\9&DC32669&3&0003";
PnPDevice device = PnPDevice.GetDeviceByInstanceId(instanceId);
// uninstall/deconfigure device
device.Uninstall();
Thread.Sleep(1000);
// trigger re-enumeration
Devcon.Refresh();// replace with the instance ID of the USB device you want to power-cycle
const string instanceId = @"USB\VID_054C&PID_0CE6\0000000000000000";
PnPDevice device = PnPDevice.GetDeviceByInstanceId(instanceId);
UsbPnPDevice usbDevice = device.ToUsbPnPDevice();
usbDevice.CyclePort();This library benefits from these awesome projects ❤ (appearance in no special order):
- Manage Windows devices in .NET, can enumerate, start, stop, disable, enable, and remove devices
- Tooling to generate metadata for Win32 APIs in the Windows SDK.
- Disable or Enable Device with Hardware ID
- C#/Win32 P/Invoke Source Generator
- Driver Store Explorer [RAPR]
- ViGEm.Management
- XMLDoc2Markdown
- ManagedDevcon
- Driver Updater
