我想使用 C# 控制台应用程序阻止 USB 端口

I want to block USB Port using C# console application

我正在尝试制作一个可以通过 code.I 修改 windows 注册表的简单程序,我尝试添加一个应用程序清单文件来为代码提供管理员权限,但似乎没有任何效果。

    try
        { 
            RegistryKey MyKey = Registry.LocalMachine.OpenSubKey("/SYSTEM/CurrentControlSet/Services/USBSTOR", true);
         //the control jumps to catch after the above line.
            MyKey.SetValue("Start", "4", RegistryValueKind.DWord);
            System.Console.WriteLine("Port locked");
            System.Console.ReadKey();
        }//throws a nullvaluerefrence exception
        catch
        {
            System.Console.WriteLine("There was an error");
            System.Console.ReadKey();
        }

MyKey.SetValue 有一个无效参数。 string 无法转换为 int.

MyKey.SetValue("Start", "4", RegistryValueKind.DWord);

将代码更改为:

MyKey.SetValue("Start", 0x4, RegistryValueKind.DWord);