25

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"

5 Answers 5

28

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

4
12

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();
4
  • 2
    I received a Additional information: Access to the registry key 'HKEY_LOCAL_MACHINE\Software\Wow6432Node\Names' is denied. error when running the snippet. Please advise.
    – Cole
    Dec 18, 2014 at 16:54
  • Run application as administrator and it should work.
    – PandaNL
    Mar 12, 2015 at 14:51
  • What if i wana use numbers? do i do key.SetValue("Name", "1");?
    – Nullify
    Mar 13, 2023 at 14:29
  • @Nullify you try like this shown in the example learn.microsoft.com/en-us/dotnet/api/… Mar 17, 2023 at 12:00
10

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");
4
  • 1
    Exactly, running this snippet above in VS or by going to /bin/Debug/App.exe and running that as Admin resulted in the below error: An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll Additional information: Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\YourCompanyName\SomeNewKey' is denied.
    – Cole
    Dec 18, 2014 at 16:59
  • @Cole: You may want to post a new question with details specific to your problem. This method for writing to Registry is tried and true and I have been using it in commercial software for years. If you post your code we can help you figure out why you get this exception. My typical pattern is: RegistryKey rk = Registry.LocalMachine.OpenSubKey(path, true); if (rk == null) { rk = Registry.LocalMachine.CreateSubKey(path);} if(rk!=null){ .. do stuff .. rk.Close(); } Dec 18, 2014 at 17:27
  • 1
    string path = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Authentication\Credential Providers"; RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(path, true); which throws An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll Additional information: Requested registry access is not allowed.
    – Cole
    Dec 18, 2014 at 23:52
  • Dude, if you want help post a new question. I did try this path and it works fine for me. You need to post a new question with details specific to your problem (what OS, what architecture: 32-bit vs 64-bit). You possibly are running into probs with UAC / elevation or permissions. Also, try a search on that specific exception, for example: stackoverflow.com/questions/562350/… Dec 19, 2014 at 1:48
0

Well you've got your answer already - I'm guessing you're running on Vista or Windows 7 (or Server 2008) and the process/user running the app doesn't have rights/permission to modify the registry.

So its not a code problem as such but a systems admin one. Build the app and run as administrator and see if that works.

1
  • I can confirm that updating HKEY_LOCAL_MACHINE with admin rights programmatically as suggested in the above code snippets and the accepted answer does not work.
    – Cole
    Dec 18, 2014 at 17:00
-1

Set the Premission Check bit to true...

Microsoft.Win32.Registry.LocalMachine.CreateSubKey("Software\\xelo\\", true);

:)

2
  • 2
    Registry.LocalMachine doesn't contain any overload method which allow CreateSubKey(String,Bool)
    – WiiMaxx
    Aug 6, 2014 at 6:56
  • OpenSubkey() uses the boolean second arg to open the key writable. What you want is this: .CreateSubKey(r,RegistryKeyPermissionCheck.ReadWriteSubTree) Mar 1, 2019 at 6:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.