C#:usb处于活动状态时禁用 [英] C# : disable while usb is active

查看:200
本文介绍了C#:usb处于活动状态时禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码启用并禁用USB端口。但是,当我单击禁用按钮时,不会禁用现有的Usb。当Usb处于活动状态时,我的老师想要禁用。如果你帮忙,我会非常感激。



当我点击禁用按钮时,不会禁用现有的Usb.I尝试了一切,但我做不到。



我尝试过:



My code is enable and disabe the usb port. But when I clicked the disable button,doesn't disable the existing Usb. My teacher wants to disabled when the Usb is active,using.I would be very grateful if you help.

When I clicked the disable button,doesn't disable the existing Usb.I tried everything but I could not.

What I have tried:

private void lockbtn_Click(object sender, EventArgs e)
{
 // Create and name your <span id="IL_AD2" class="IL_AD">button</span> on form as lockbtn

            try
            {

                string path = "SYSTEM\\CurrentControlSet\\services\\USBSTOR\\";
                RegistryKey RK = Registry.LocalMachine.OpenSubKey(path, true);

                RK.SetValue("Start", "4", RegistryValueKind.DWord);
                status.Text = "Status : All Usb Ports Locked !";
            }
            catch (Exception ex)
            {

                 MessageBox.Show(" Should be Run as Adminstrator","Stop");

            }
        }

        private void unlockbtn_Click(object sender, EventArgs e)
        {
            //Name your <span id="IL_AD3" class="IL_AD">unlock</span> button as unlockbtn

            status.Text = "Status : All Usb Ports Unlocked !";
            string path = "SYSTEM\\CurrentControlSet\\services\\USBSTOR\\";
            RegistryKey RK = Registry.LocalMachine.OpenSubKey(path, true);

            RK.SetValue("Start", "3", RegistryValueKind.DWord);
        }

 
    }
}

推荐答案

所有你'重做是改变注册表设置。你没有禁用任何东西。



这个键/值是政策的目标,仅此而已。您所做的只是禁用USB存储驱动程序,以防止加载。它不会告诉存储驱动器自行卸载。一旦加载,它就会在Windows会话中保持加载状态。您必须设置该值并重新启动Windows才能使其生效。



请注意,该值不会禁用USB端口。它只能阻止USB存储(USB驱动设备)驱动程序加载。端口仍适用于所有其他USB设备。
All you're doing is changing a registry setting. You're not disabling anything.

This key/value is the target of a policy, nothing more. All you're doing is disabling USB Storage driver, preventing it from loading. It will NOT tell the storage drive to unload itself. Once it's loaded it stays loaded for that session of Windows. You have to set that value and restart Windows for it to take effect.

Mind you, that value doesn't disable the USB Ports. It only prevents the USB Storage (USB drive devices) driver from loading. The ports still work for all other USB devices.


这篇关于C#:usb处于活动状态时禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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