C# 禁用/启用 USB 端口 [英] C# Disable/Enable USB ports

查看:215
本文介绍了C# 禁用/启用 USB 端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一次禁用/启用所有 USB 端口.
我尝试了以下方法:

更改注册表(具有管理员权限):

//禁用USB存储...Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 4, Microsoft.Win32.RegistryValueKind.DWord);//启用USB存储...Microsoft.Win32.Registry.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR", "Start", 3, Microsoft.Win32.RegistryValueKind.DWord);

我还尝试了 C# 的硬件帮助程序库 - 甚至没有检测到设备
Win32 API 函数以编程方式启用/禁用设备>

如果重要的话,我使用的是 Windows 8.1 64 位,如果再次重要,我会使用平台目标 = 任何 CPU 编译代码.


还是不行.
我想阻止/禁用所有 USB 设备,然后甚至在将来连接,而不仅仅是当前.
有人可以帮我吗?

非常感谢您的帮助.

解决方案

该注册表项阻止加载 USBSTOR 驱动程序.如果它已经在使用,它不会卸载它.所以你要等到重启后才能看到任何效果.

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.

解决方案

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.

这篇关于C# 禁用/启用 USB 端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆