我想使用C#console应用程序阻止USB端口

时间:2016-08-07 07:20:03

标签: c# usb registry console-application

我正在尝试创建一个可以通过代码修改Windows注册表的简单程序。我尝试添加一个Application Manifest文件来为代码提供管理员权限,但似乎没有任何工作。

    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();
        }

1 个答案:

答案 0 :(得分:0)

MyKey.SetValue的参数无效。 string无法转换为int

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

将代码更改为:

MyKey.SetValue("Start", 0x4, RegistryValueKind.DWord);
相关问题