{"id":16783885,"url":"https://github.com/nefarius/nefarius.utilities.devicemanagement","last_synced_at":"2025-03-17T02:31:47.775Z","repository":{"id":39997539,"uuid":"448667028","full_name":"nefarius/Nefarius.Utilities.DeviceManagement","owner":"nefarius","description":"Managed wrappers around SetupAPI, Cfgmgr32, NewDev and DrvStore Windows APIs.","archived":false,"fork":false,"pushed_at":"2025-03-11T13:12:02.000Z","size":439,"stargazers_count":33,"open_issues_count":0,"forks_count":10,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-11T14:24:04.462Z","etag":null,"topics":["csharp","devices","dotnet","hid","setupapi","usb","windows"],"latest_commit_sha":null,"homepage":"https://discord.nefarius.at","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nefarius.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"nefarius","patreon":"nefarius","liberapay":"Nefarius","custom":["https://www.buymeacoffee.com/Nefarius","https://paypal.me/NefariusMaximus","https://docs.nefarius.at/Donations/"]}},"created_at":"2022-01-16T20:36:45.000Z","updated_at":"2025-03-11T13:11:58.000Z","dependencies_parsed_at":"2023-02-13T02:31:00.327Z","dependency_job_id":"9f1e2917-edf3-4281-ab66-fae387c3c916","html_url":"https://github.com/nefarius/Nefarius.Utilities.DeviceManagement","commit_stats":null,"previous_names":[],"tags_count":89,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nefarius%2FNefarius.Utilities.DeviceManagement","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nefarius%2FNefarius.Utilities.DeviceManagement/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nefarius%2FNefarius.Utilities.DeviceManagement/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nefarius%2FNefarius.Utilities.DeviceManagement/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nefarius","download_url":"https://codeload.github.com/nefarius/Nefarius.Utilities.DeviceManagement/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243841202,"owners_count":20356441,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","devices","dotnet","hid","setupapi","usb","windows"],"created_at":"2024-10-13T07:52:38.126Z","updated_at":"2025-03-17T02:31:47.770Z","avatar_url":"https://github.com/nefarius.png","language":"C#","funding_links":["https://github.com/sponsors/nefarius","https://patreon.com/nefarius","https://liberapay.com/Nefarius","https://www.buymeacoffee.com/Nefarius","https://paypal.me/NefariusMaximus","https://docs.nefarius.at/Donations/"],"categories":[],"sub_categories":[],"readme":"# \u003cimg src=\"assets/NSS-128x128.png\" align=\"left\" /\u003eNefarius.Utilities.DeviceManagement\n\n[![.NET](https://github.com/nefarius/Nefarius.Utilities.DeviceManagement/actions/workflows/build.yml/badge.svg)](https://github.com/nefarius/Nefarius.Utilities.DeviceManagement/actions/workflows/build.yml)\n![Requirements](https://img.shields.io/badge/Requires-.NET%20Standard%202.0-blue.svg)\n[![Nuget](https://img.shields.io/nuget/v/Nefarius.Utilities.DeviceManagement)](https://www.nuget.org/packages/Nefarius.Utilities.DeviceManagement/)\n[![Nuget](https://img.shields.io/nuget/dt/Nefarius.Utilities.DeviceManagement)](https://www.nuget.org/packages/Nefarius.Utilities.DeviceManagement/)\n\nManaged wrappers around SetupAPI, Cfgmgr32, NewDev and DrvStore Windows APIs.\n\n## Features\n\n- Listen for device plugin and unplug events **without depending on WinForms or WPF**\n- Enumerate and remove elements from the Driver Store\n- Convert various notations (Symlink to Instance ID etc.)\n- Get and set\n  various [unified device properties](https://learn.microsoft.com/en-us/windows-hardware/drivers/install/unified-device-property-model--windows-vista-and-later-)\n- Enumerate devices (present and absent)\n- Hot-swap device function drivers\n- Add/Remove/Clear class filters\n- Power-cycle USB hub ports\n- Install/update drivers\n- Uninstall devices\n\n## Documentation\n\n[Link to API docs](docs/index.md).\n\n### Generating documentation\n\n- `dotnet build -c:Release`\n- `dotnet tool install --global Nefarius.Tools.XMLDoc2Markdown`\n- `xmldoc2md .\\bin\\netstandard2.0\\Nefarius.Utilities.DeviceManagement.dll .\\docs\\`\n\n## Examples\n\nSome usage examples of the core library features are presented below.\n\n### Enumerate all USB devices\n\nThe `Devcon` utility class offers helper methods to find devices.\n\n```csharp\nvar instance = 0;\n// enumerate all devices that export the GUID_DEVINTERFACE_USB_DEVICE interface\nwhile (Devcon.FindByInterfaceGuid(DeviceInterfaceIds.UsbDevice, out var path,\n           out var instanceId, instance++))\n{\n    Console.WriteLine($\"Path: {path}, InstanceId: {instanceId}\");\n\n    var usbDevice = PnPDevice\n        .GetDeviceByInterfaceId(path)\n        .ToUsbPnPDevice();\n\n    Console.WriteLine($\"Got USB device {usbDevice.InstanceId}\");\n}\n```\n\n### Listen for new and removed USB devices\n\nOne or more instances of the `DeviceNotificationListener` can be used to listen for plugin and unplug events of various\ndevices. This class has no dependency on WinForms or WPF and works in Console Applications and Windows Services alike.\n\n```csharp\nvar listener = new DeviceNotificationListener();\n\nlistener.DeviceArrived += Console.WriteLine;\nlistener.DeviceRemoved += Console.WriteLine;\n\n// start listening for plugins or unplugs of GUID_DEVINTERFACE_USB_DEVICE interface devices\nlistener.StartListen(DeviceInterfaceIds.UsbDevice);\n```\n\n### Get all driver packages in the Driver Store\n\n```csharp\nvar allDriverPackages = DriverStore.ExistingDrivers.ToList();\n```\n\n### Remove all copies of `mydriver.inf` from the Driver Store\n\n```csharp\nforeach (var driverPackage in allDriverPackages.Where(p =\u003e p.Contains(\"mydriver.inf\", StringComparison.OrdinalIgnoreCase)))\n{\n    DriverStore.RemoveDriver(driverPackage);\n}\n```\n\n### Get Instance ID from a symbolic link (device path)\n\n```csharp\n// e.g. the path \"\\\\?\\HID#VID_045E\u0026PID_028E\u0026IG_00#3\u002631f0e99d\u00260\u00260000#{4d1e55b2-f16f-11cf-88cb-001111000030}\"\n// gets translated to instanceId \"HID\\VID_045E\u0026PID_028E\u0026IG_00\\3\u002631f0e99d\u00260\u00260000\"\nvar instanceId = PnPDevice.GetInstanceIdFromInterfaceId(path);\n```\n\n### Get PnPDevice object from symbolic link\n\n```csharp\n// example path: \"\\\\?\\HID#VID_046D\u0026PID_C33F\u0026MI_01\u0026COL02#B\u002631580538\u00260\u00260001#{4D1E55B2-F16F-11CF-88CB-001111000030}\"\nPnPDevice device = PnPDevice.GetDeviceByInterfaceId(path);\n```\n\n### Add a driver service to a device class' upper filters\n\n```csharp\nstring service = \"HidHide\";\nDeviceClassFilters.AddUpper(DeviceClassIds.XnaComposite, service);\n```\n\n### Replace a device instance's function driver with WinUSB\n\n```csharp\nconst string instanceId = @\"USB\\VID_054C\u0026PID_0CE6\u0026MI_03\\9\u0026DC32669\u00263\u00260003\";\n        \nPnPDevice device = PnPDevice.GetDeviceByInstanceId(instanceId);\n\n// required for the next call to succeed\ndevice.InstallNullDriver();\n\nThread.Sleep(1000);\n\n// accepts any INF already present in C:\\Windows\\INF directory\ndevice.InstallCustomDriver(\"winusb.inf\");\n```\n\n### Uninstall device/revert function driver to default\n\n```csharp\nconst string instanceId = @\"USB\\VID_054C\u0026PID_0CE6\u0026MI_03\\9\u0026DC32669\u00263\u00260003\";\n        \nPnPDevice device = PnPDevice.GetDeviceByInstanceId(instanceId);\n\n// uninstall/deconfigure device\ndevice.Uninstall();\n\nThread.Sleep(1000);\n\n// trigger re-enumeration\nDevcon.Refresh();\n```\n\n### Cycle hub port a given USB device is attached to\n\n```csharp\nif (Devcon.FindByInterfaceGuid(HostRadio.DeviceInterface, out PnPDevice radioDevice))\n{\n    UsbPnPDevice? usbHostRadio = radioDevice.ToUsbPnPDevice();\n\n    usbHostRadio.CyclePort();\n}\n```\n\n## Sources \u0026 3rd party credits\n\nThis library benefits from these awesome projects ❤ (appearance in no special order):\n\n- [Manage Windows devices in .NET, can enumerate, start, stop, disable, enable, and remove devices](https://gist.github.com/jborean93/01ba060ac9043a7b997d396de7aa009e)\n- [Tooling to generate metadata for Win32 APIs in the Windows SDK.](https://github.com/microsoft/win32metadata)\n- [Disable or Enable Device with Hardware ID](https://gist.github.com/3735943886/f47c355738e3dd7975fe0aa1abd67445)\n- [C#/Win32 P/Invoke Source Generator](https://github.com/microsoft/CsWin32)\n- [Driver Store Explorer [RAPR]](https://github.com/lostindark/DriverStoreExplorer)\n- [ViGEm.Management](https://github.com/ViGEm/ViGEm.Management)\n- [XMLDoc2Markdown](https://charlesdevandiere.github.io/xmldoc2md/)\n- [ManagedDevcon](https://github.com/nefarius/ManagedDevcon)\n- [Driver Updater](https://github.com/WOA-Project/DriverUpdater)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnefarius%2Fnefarius.utilities.devicemanagement","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnefarius%2Fnefarius.utilities.devicemanagement","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnefarius%2Fnefarius.utilities.devicemanagement/lists"}