C# 如何禁用系统设备?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/252185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I disable a system device?
提问by Cheetah
Is there any way to disable a system device from C#.NET.
有什么方法可以从 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?
我假设有一个可以调用的 WinAPI 函数,但我不知道是哪个函数?
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.
我需要在 Windows XP 和 Vista(x86 和 x64)下工作。我看过一个叫做“DevCon”的东西,但它似乎不适用于 Vista x64。
Thanks.
谢谢。
For the answer see here: How do I disable a system device programatically?
有关答案,请参见此处:如何以编程方式禁用系统设备?
回答by Mel Green
This article at CodeProject has some really nifty C# code for accomplishing this:
CodeProject 上的这篇文章有一些非常漂亮的 C# 代码来完成这个:
http://www.codeproject.com/KB/cs/HardwareHelper.aspx
http://www.codeproject.com/KB/cs/HardwareHelper.aspx
In the end you simply make a call such as:
最后,您只需拨打电话,例如:
HH_Lib hwh = new HH_Lib();
hwh.SetDeviceState("MyDevice", /*Enable*/ true);
hwh.SetDeviceState("MyDevice", /*Disable*/ false);
回答by Matthew Savage
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.
为了进一步解释 Mel Green 的答案,硬件助手库使用互操作来调用 Windows API (setupapi),特别是 SetupDiSetClassInstallParams 方法。当然,这里还有其他调用用于查找设备详细信息等。
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)
有关更多详细信息,请查看http://www.pinvoke.net(以及具体调用:http: //pinvoke.net/default.aspx/setupapi.SetupDiSetClassInstallParam)
回答by Brian Hasden
I know this is an old question but I came across it with the same problem. To solve it, I had to use the new DevCon available at the TechNet wiki. The CodeProject article was helpful but in the end it was easier to just rely on the updated DevCon. Unfortunately it appears to only be available in the Windows Driver Kitwhich is over 600mb.
我知道这是一个老问题,但我遇到了同样的问题。为了解决这个问题,我不得不使用TechNet wiki 上提供的新 DevCon 。CodeProject 文章很有帮助,但最终只依赖更新的 DevCon 会更容易。不幸的是,它似乎只在超过 600mb的Windows 驱动程序工具包中可用。