views:

1541

answers:

3

Is there any way to disable a system device from C#.NET.

Basically emulating when you go to Device Manager and disable a device that way?

I am assuming there is a WinAPI function that I can invoke, but I don't know which one it is?

The reason I need to do this is that I need to disable and straight after enable the device again.

I need to work in Windows XP and Vista (x86 & x64). I had a look at something called "DevCon" but it doesn't seem to work on Vista x64.

Thanks.

+5  A: 
Mel Green
This doesn't work for Vista x64 (see my reply to the question).Thanks.
Ben
Doesn't work for Server 2008 x86 either
Dale Halliwell
+3  A: 

To futher Mel Green's answer, the hardware helper library uses interop to call the Windows API (setupapi), specifically the SetupDiSetClassInstallParams method. Of course there are other calls here which are used to look up the device details etc.

One thing to consider that if you are running as a standard user or unprivileged account you will not be able to use this to control (enable/disable etc) hardware devices.

For more details have a look at http://www.pinvoke.net (and for the specific call: http://pinvoke.net/default.aspx/setupapi.SetupDiSetClassInstallParam )

Matthew Savage
A: 

Thank you for the responses.

This doesn't appear to work on my development machine (Vista x64).

I tried setting the build settings to x86, but that didn't appear to work either.

In the test application on CodeProject, it can list the devices fine but can't disable/enable them.

I have ensured that I am running Visual Studios in administrator mode on an administrator account.

Am I doing something wrong or does the library not work with x64?

Edit: After a bit of research, it appears that DevCon use the same API calls...i think anyways

Ben