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"
Non-admin and unelevated admin users don't have rights to modify the HKEY_LOCAL_MACHINE key. Run the program 'as administrator'.
Below code to create key in the registry.
Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("Software\\Wow6432Node\\Names");
key.SetValue("Name", "Isabella");
key.Close();
Even when admin I don't think you can create new keys off LocalMachine. Make sure that you do
Registry.LocalMachine.CreateSubKey(@"SOFTWARE\YourCompanyName\SomeNewKey");
and not
Registry.LocalMachine.CreateSubKey("SomeNewKey");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With