I am looking for way to disable a system device (either USB or internal) in C#.NET given either the PID & VID or the device name.
After searching I found Hardware Helper Library for C# on CodeProject.
But I need something that will work on XP, Vista & Windows 7 (both x86 and x64 operating systems)...the project I linked only works with XP and Vista x86...even when running the application with administrator privileges.
Does anyone know of a solution that would work on all operating systems?
I managed to solve this Problem using DevManView.exe (freeware):
Download DevManView.exe and put the .exe file somewhere: http://www.nirsoft.net/utils/device_manager_view.html
Go to your Device Manager and search for the Name of the device you want to enable/disable.
In C#, create and start a new process for disabling the device (using the name of the device you found in the device manager).
And enable it again.
Looks like there were two things in drf's version that were giving you trouble. Your x64 problem was caused by SP_DEVINFO_DATA having a uint where an IntPtr was needed. The second is that the SetupDiGetDevicePropertyW function is Vista+ and won't run on XP.
Here's a version with those two issues corrected. I tested as x86 and AnyCPU on XPx86 and Win7X64.
It can be done using P/Invoke methods on the Windows Setup API. The API functions have been in Windows since W2K. The API calls may require administrative rights. Below is some rough code for using these APIs to disable (as well as enable) devices in C#, which allow invoking:
This works on Windows 10 on Dell inspi 15.
It can be done using P/Invoke methods on the Windows Setup API. The API functions have been in Windows since W2K. The API calls may require administrative rights. Below is some rough code for using these APIs to disable (as well as enable) devices in C#, which allow invoking:
The first parameter is a filter that is passed the hardware ID. This disables or enables the first device matching the provided filter. There is no effect if the device is already in the target state.
The code follows.
I've built upon the XP-compatible answer and improved it a bit by adding a
SafeDeviceInformationSetHandle
class and performing some general code cleanup/refactoring, and am adding it here for posterity: