DeviceManagementPropertiesGenerator 1.0.0

dotnet add package DeviceManagementPropertiesGenerator --version 1.0.0
NuGet\Install-Package DeviceManagementPropertiesGenerator -Version 1.0.0
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="DeviceManagementPropertiesGenerator" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DeviceManagementPropertiesGenerator --version 1.0.0
#r "nuget: DeviceManagementPropertiesGenerator, 1.0.0"
#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.
// Install DeviceManagementPropertiesGenerator as a Cake Addin
#addin nuget:?package=DeviceManagementPropertiesGenerator&version=1.0.0

// Install DeviceManagementPropertiesGenerator as a Cake Tool
#tool nuget:?package=DeviceManagementPropertiesGenerator&version=1.0.0

<img src="assets/NSS-128x128.png" align="right" />

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 was computed.  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. 
.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 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  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.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 104 3/11/2024