Nefarius.Utilities.DeviceManagement 4.0.0-pre002

Prefix Reserved
This is a prerelease version of Nefarius.Utilities.DeviceManagement.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Nefarius.Utilities.DeviceManagement --version 4.0.0-pre002
                    
NuGet\Install-Package Nefarius.Utilities.DeviceManagement -Version 4.0.0-pre002
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Nefarius.Utilities.DeviceManagement" Version="4.0.0-pre002" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nefarius.Utilities.DeviceManagement" Version="4.0.0-pre002" />
                    
Directory.Packages.props
<PackageReference Include="Nefarius.Utilities.DeviceManagement" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Nefarius.Utilities.DeviceManagement --version 4.0.0-pre002
                    
#r "nuget: Nefarius.Utilities.DeviceManagement, 4.0.0-pre002"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Nefarius.Utilities.DeviceManagement@4.0.0-pre002
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Nefarius.Utilities.DeviceManagement&version=4.0.0-pre002&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Nefarius.Utilities.DeviceManagement&version=4.0.0-pre002&prerelease
                    
Install as a Cake Tool

<img src="assets/NSS-128x128.png" align="left" />Nefarius.Utilities.DeviceManagement

.NET Requirements Nuget Nuget

Managed wrappers around SetupAPI, Cfgmgr32, NewDev and DrvStore native APIs on Windows.

Features

  • 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

Documentation

Link to API docs.

Generating documentation

  • dotnet build -c:Release
  • dotnet tool install --global Nefarius.Tools.XMLDoc2Markdown
  • xmldoc2md .\bin\netstandard2.0\Nefarius.Utilities.DeviceManagement.dll .\docs\

Examples

Some usage examples of the core library features are presented below.

Enumerate all USB devices

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}");
}

Listen for new and removed USB devices

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);

Get all driver packages in the Driver Store

var allDriverPackages = DriverStore.ExistingDrivers.ToList();

Remove all copies of mydriver.inf from the Driver Store

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);

Add a driver service to a device class' upper filters

string service = "HidHide";
DeviceClassFilters.AddUpper(DeviceClassIds.XnaComposite, service);

Replace a device instance's function driver with WinUSB

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");

Uninstall device/revert function driver to default

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();

Cycle hub port a given USB device is attached to

if (Devcon.FindByInterfaceGuid(HostRadio.DeviceInterface, out PnPDevice radioDevice))
{
    UsbPnPDevice? usbHostRadio = radioDevice.ToUsbPnPDevice();

    usbHostRadio.CyclePort();
}

Sources & 3rd party credits

This library benefits from these awesome projects ❤ (appearance in no special order):

Product 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 is compatible.  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 is compatible.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.6.2

    • No dependencies.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net8.0

    • No dependencies.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on Nefarius.Utilities.DeviceManagement:

Package Downloads
Nefarius.Drivers.HidHide

Managed API for configuring HidHide.

Wujek_Dualsense_API

Wujek Dualsense API is a .NET library designed to interface with the PlayStation DualSense controller, providing functionalities to control its various features such as haptic feedback, LEDs, triggers, and more.

Nefarius.Utilities.Bluetooth

Utility library for unconventional Bluetooth tasks on Windows.

O.Usb

Package Description

Oakrey.Usb

A .NET library for managing USB devices, featuring communication, device monitoring, and event handling functionalities.

GitHub repositories (7)

Showing the top 7 popular GitHub repositories that depend on Nefarius.Utilities.DeviceManagement:

Repository Stars
Valkirie/HandheldCompanion
ControllerService
CircumSpector/DS4Windows
A reimagination of DS4Windows.
project-sbc/Handheld-Control-Panel
nefarius/Legacinator
The one and only Legacinator.
InvoxiPlayGames/FestivalInstrumentMapper
Maps most existing Rock Band / Guitar Hero instruments as Xbox One instruments for Fortnite Festival.
Struggleton/Delfinovin
An XInput solution for Gamecube Controllers
project-sbc/Handheld-Hardware-Tools
Software that compliments Windows laptops/tablets/gaming handhelds to allow easy control of TDP, brightness, cpu clock, etc.
Version Downloads Last Updated
5.1.0 1,620 2/23/2025
5.0.0 2,089 11/9/2024
5.0.0-pre001 108 11/9/2024
4.0.3 182 11/7/2024
4.0.3-pre001 98 11/7/2024
4.0.2 307 10/30/2024
4.0.1 790 10/1/2024
4.0.0 1,575 9/20/2024
4.0.0-pre002 100 9/20/2024
4.0.0-pre001 114 9/20/2024
3.23.0 624 8/17/2024
3.22.0 3,748 7/7/2024
3.21.0 181 7/4/2024
3.20.0 166 7/4/2024
3.19.0 319 6/30/2024
3.19.0-pre3 145 6/28/2024
3.19.0-pre2 124 6/27/2024
3.18.1 1,574 3/11/2024
3.17.406 2,551 8/29/2023
3.17.403 335 8/27/2023
3.17.401-pre 179 8/26/2023
3.16.384-pre 216 8/23/2023
3.14.305 2,466 5/14/2023
3.13.303-pre 207 5/14/2023
3.12.297-pre 276 5/13/2023
3.11.295-pre 232 5/12/2023
3.10.293-pre 279 5/9/2023
3.9.255 1,771 4/9/2023
3.8.228 1,276 2/2/2023
3.8.225-pre 239 2/1/2023
3.6.213 842 1/12/2023
3.6.211 382 1/12/2023
3.5.209 589 12/26/2022
3.4.206 458 12/20/2022
3.4.204-pre 230 12/19/2022
3.4.202-pre 208 12/19/2022
3.3.196 756 10/16/2022
3.2.193 479 10/16/2022
3.2.191 477 10/16/2022
3.2.189 494 10/16/2022
3.2.186 485 10/16/2022
3.2.184 753 9/29/2022
3.2.175 577 9/12/2022
3.1.173 473 9/12/2022
3.1.171 486 9/12/2022
3.0.168 508 9/10/2022
3.0.165-pre 298 9/9/2022
2.9.116 565 8/25/2022
2.8.110 1,361 7/19/2022
2.8.107 807 6/15/2022
2.7.104 521 6/14/2022
2.6.102 681 6/2/2022
2.6.100 549 5/26/2022
2.6.98 577 5/24/2022
2.4.94 590 5/19/2022
2.4.91 562 5/14/2022
2.4.89 501 5/14/2022
2.3.87 527 5/13/2022
2.2.85 555 5/11/2022
2.1.76 528 5/11/2022
2.1.74 535 5/10/2022
2.0.71 546 5/10/2022
2.0.68 568 5/8/2022
2.0.66 526 5/8/2022
2.0.60 537 5/8/2022
1.6.50 563 5/6/2022
1.6.48-beta 263 5/6/2022
1.4.42-beta 1,098 2/1/2022
1.3.28-beta 292 1/30/2022
1.2.24-beta 272 1/30/2022
1.2.22-beta 268 1/30/2022
1.1.20-beta 280 1/30/2022
1.0.18-beta 280 1/30/2022
1.0.15-beta 438 1/17/2022
1.0.13-beta 269 1/17/2022
1.0.11-beta 276 1/17/2022
1.0.6-beta 284 1/16/2022
1.0.4-beta 284 1/16/2022
1.0.2-beta 289 1/16/2022