Ismael Oliveira

Ismael Oliveira

  • 2.1k
  • 99
  • 3.9k

Changing Windows Registry not working

Aug 31 2022 10:53 PM

Hi. I'm working with Visual Studio on a application that changes a windows registry. There is a small forms that requests an address in a text box. The contents of the text box wil be writen is the registry. I need the key to be LocalMachine and here is a snippet of this code:

        private void button1_Click(object sender, EventArgs e)
            RegistryKey chaveBase, chave;
            chaveBase = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            chave = chaveBase.CreateSubKey("Software\\Antares", true);
            chave.SetValue("Endereço", textBox1.Text); 

The problem is that I receive the message: "System.UnauthorizedAccessException: 'Access to the registry key 'HKEY_LOCAL_MACHINE\Software\Antares' is denied".
If I change LocalMachine for CurrentUser, it works fine. What should I do to access LocalMachine?

Answers (1)