# C# 修改注册表禁用U盘

代码

C#
private void LockUsb()
{
    try
    {
        if (OperatingSystem.IsWindows())
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\\CurrentControlSet\\Services\\USBSTOR", true);
            if (key != null)
            {
                key.SetValue("Start", 4);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"lock usb {ex}");
    }

}
private void UnLockUsb()
{
    try
    {
        if (OperatingSystem.IsWindows())
        {

            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\\CurrentControlSet\\Services\\USBSTOR", true);
            if (key != null)
            {
                key.SetValue("Start", 3);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine($"unlock usb {ex}");
    }

}