Managed wrappers around SetupAPI, Cfgmgr32, NewDev and DrvStore native APIs on Windows.
Some usage examples of the core library features are presented below.
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(Guid.Parse("{a5dcbf10-6530-11d2-901f-00c04fb951ed}"), 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(Guid.Parse("{a5dcbf10-6530-11d2-901f-00c04fb951ed}"));
var allDriverPackages = DriverStore.ExistingDrivers.ToList();
foreach (var driverPackage in allDriverPackages.Where(p => p.Contains("mydriver.inf", StringComparison.OrdinalIgnoreCase)))
{
DriverStore.RemoveDriver(driverPackage);
}