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"

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Angel.King.47
  • 7,922
  • 14
  • 60
  • 85

5 Answers5

28

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

Kyle Alons
  • 6,955
  • 2
  • 33
  • 28
  • 15
    Try passing true to the 2nd parameter of the OpenSubKey call or using the return value of CreateSubKey. http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.opensubkey.aspx http://msdn.microsoft.com/en-us/library/ad51f2dx%28v=VS.100%29.aspx – Kyle Alons Apr 27 '10 at 17:32
  • 2
    Thanks Kyle, you have to do OpenSubKey("KeyName", true) Where the true means writable :D, Edit your answer to include that – Angel.King.47 Apr 27 '10 at 17:36
  • @segfault, Did that same problem, need write access on the function – Angel.King.47 Apr 27 '10 at 17:37
  • The answer, as it stands in incomplete/wrong. The comments are right. – TaW Jan 14 '20 at 12:51
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();
Dinesh Haraveer
  • 1,784
  • 3
  • 31
  • 54
  • 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 '14 at 16:54
  • Run application as administrator and it should work. – PandaNL Mar 12 '15 at 14:51
  • What if i wana use numbers? do i do `key.SetValue("Name", "1");`? – Nullify Mar 13 '23 at 14:29
  • @Nullify you try like this shown in the example https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registrykey.setvalue?view=net-7.0 – Dinesh Haraveer Mar 17 '23 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");
dandan78
  • 13,328
  • 13
  • 64
  • 78
nothingisnecessary
  • 6,099
  • 36
  • 60
  • 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 '14 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(); }` – nothingisnecessary Dec 18 '14 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 '14 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: http://stackoverflow.com/questions/562350/requested-registry-access-is-not-allowed – nothingisnecessary Dec 19 '14 at 01: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.

Murph
  • 9,985
  • 2
  • 26
  • 41
  • 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 '14 at 17:00
-1

Set the Premission Check bit to true...

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

:)

  • 2
    Registry.LocalMachine doesn't contain any overload method which allow [CreateSubKey(String,Bool)](http://msdn.microsoft.com/en-us/library/Microsoft.Win32.RegistryKey.CreateSubKey(v=vs.110).aspx) – WiiMaxx Aug 06 '14 at 06:56
  • OpenSubkey() uses the boolean second arg to open the key writable. What you want is this: `.CreateSubKey(r,RegistryKeyPermissionCheck.ReadWriteSubTree)` – jrypkahauer Mar 01 '19 at 06:09