Search code examples
c#registry

C# Create Values in Registry Local Machine


The following code is not working for me:

public bool createRegistry()
{
    if (!registryExists())
    {
        Microsoft.Win32.Registry.LocalMachine.CreateSubKey("Software\\xelo\\");

        Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\xelo").SetValue("hostname", (string)hostname, Microsoft.Win32.RegistryValueKind.String);


        return true;
    }
    else
    {
        return updateRegistry();
    }
}

Exception:

System.UnauthorizedAccessException | "Cannot write to the registry key"


Solution

  • Non-admin and unelevated admin users don't have rights to modify the HKEY_LOCAL_MACHINE key. Run the program 'as administrator'.