-2

My code here:

private void button1_Click(object sender, EventArgs e)
{
    RegistryKey Hklm = Registry.LocalMachine;
    RegistryKey HkSoftware = Hklm.OpenSubKey("Software");
    RegistryKey HkMicrosoft = HkSoftware.OpenSubKey("Microsoft");
    RegistryKey HkWindows = HkMicrosoft.OpenSubKey("Windows");
    RegistryKey HkMine = HkWindows.CreateSubKey("WindowsUpdate", RegistryKeyPermissionCheck.ReadWriteSubTree);
    HkMine.SetValue("NoAutoRebootWithLoggedOnUsers", 1);
}

Screenshot 1

In visual studio it given to me it's error:

Community
  • 1
  • 1

1 Answers1

0

Applications not running with admin rights (elevated) are not allowed to write to HKEY_LOCAL_MACHINE. Microsoft has been warning about this for years. When you debug an application in Visual Studio, the application is started with the same user rights Visual Studio is running under.

The solution is to run the Visual Studio as administrator for debugging.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139