C# Disable/Enable USB ports

元气小坏坏 提交于 2019-12-13 05:31:30

问题


I want to disable/enable all usb ports at once.
I tried the following:

Changed the registry (with admin privileges):

//disable USB storage...
Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 4, Microsoft.Win32.RegistryValueKind.DWord); 

//enable USB storage...
Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 3, Microsoft.Win32.RegistryValueKind.DWord);

I also tried Hardware Helper Library for C# - even not detecting the devices
and Win32 API function to programmatically enable/disable device

I am using windows 8.1 64bit if it matters, and I compile the code with platform target = any CPU if it matters again.

EDIT:
Still not working.
I want to block/disable all the usb devices then even connect in the future and not only the current.
Can anyone help me?

Would appreciate your help.


回答1:


That registry key prevents the USBSTOR driver from loading. It won't unload it if it is already in use. So you will not see any effect until after reboot.



来源:https://stackoverflow.com/questions/20478042/c-sharp-disable-enable-usb-ports

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!