[.net] Enable/Disable USB port

Started by
3 comments, last by Moe 15 years, 3 months ago
Hi How can I enable or disable USB ports in C#? Thanks in advance
Advertisement
According to this Microsoft KB article, there is a registry key for this.

To disable:
          Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\USBSTOR", true).SetValue("Start", 4);


To re-enable:
          Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\USBSTOR", true).SetValue("Start", 3);


However, the above code only for mass storage devices, not other devices such as keyboards and mice connected to the USB bus.
There is no easy way to enable/disable hardware using C# that I know of. I remember because I looked up how to disable the NIC using C# and it required a bunch of complex things to be done. Since I was merely costing out how much it would be to code such a thing, I told my boss that it wasn't worth it. The registry key solution is best if that's what you're trying to do. Otherwise you might be better off using C++/CLI.
There biggest chance is that the solution is there for you through the Win32 API in C. Which means, you'll have to P/Invoke the required functions/structures and then call those functions from your C# application.

It probably doesn't matter much if you write a C DLL that does all this stuff, and then P/Invoke the library's specific function on enabling/disabling the USB port or writing all the code in C#(However, I expect the code to be more simple in C).

Now, the question arises: Why do you want to disable USB ports? If you want to do this on a system scale because of security, why not look into hardware profiles or group policies? These are much more efficient at these things.

Toolmaker

Why exactly do you want to disable USB ports?

This topic is closed to new replies.

Advertisement