简体   繁体   中英

I want to block USB Port using C# console application

I am trying to make a simple program that can modify windows registry through code.I have tried adding an Application Manifest File to provide admin privileges to the code but nothing seems to work.

    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 has an invalid parameter. string cannot be converted to int .

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

Change the code to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM