39

I am getting error cannot write to the registry key when i am trying to save my keys in the registry .

//Here is my code .

Note : I tried to run as an Administartor assuming some permission problems still getting the same error ....

private const string RegistryKeyName = "Skms";
private readonly RegistryKey SoftwareKey = Registry.LocalMachine.OpenSubKey("SOFTWARE");

public KeyManagementRegistryKeyChangeImpl(bool writeable)
    {
        this.writable = writeable;
        RegistryKey skms; 
        if (Environment.Is64BitOperatingSystem == true) 
        {
            skms = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(RegistryKeyName,true);

        }
        else
        {
            skms = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
        }

        if (null == skms)
        {
            skms = SoftwareKey.CreateSubKey(RegistryKeyName, RegistryKeyPermissionCheck.ReadWriteSubTree);
        }

        if(skms == null)
        {
            throw new System.ArgumentException(string.Format(CultureInfo.InvariantCulture, 
                @"Registry Key 'HKEY_LOCAL_MACHINE\SOFTWARE\{0}' not found or created",
                RegistryKeyName));
        }

        Decryptor decryptor = Decryptor.Create();
62071072SP
  • 1,963
  • 2
  • 19
  • 38

4 Answers4

105

Try this:

RegistryKey skms = SoftwareKey.OpenSubKey(RegistryKeyName, true);

The second parameter should be set to true if you need write access to the key.

-EDIT-

On 64-bit system, you can try this (if you are using .Net 4):

private readonly RegistryKey SoftwareKey = 
    RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).
    OpenSubKey("SOFTWARE");
Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
  • @ Edwin de Koning Registry Key 'HKEY_LOCAL_MACHINE\SOFTWARE\Skms' not found or created I am getting this new error after i try giving OpenSubKey – 62071072SP Aug 26 '11 at 09:44
  • @ShaliniPavan: Try to open the "SOFTWARE" key with the 'true' parameter as well. – Edwin de Koning Aug 26 '11 at 09:50
  • @ Edwin de Koning : I am still getting the same error and this been working fine in 32-bit systems only for 64-bit systems it is throwing me error – 62071072SP Aug 26 '11 at 09:57
  • Thanks @Edwin de Koning : but no luck – 62071072SP Aug 26 '11 at 11:58
  • "but no luck". If you gave us some helpful information, I'd try to help. – David Heffernan Aug 26 '11 at 12:50
  • I mean to say still i am getting same error even if i try to place piece of code given my you in my application. It says Cannot write to the registry key and when i check SoftwareKey.GetSubKeyNames() the "skms" is not in the list . I am not able to figure it out actually . – 62071072SP Aug 26 '11 at 13:09
  • 2
    @EdwindeKoning you definitely solved my issue. Who would have thought there would be a true parameter just so I can write – MyKuLLSKI Jan 31 '12 at 07:06
  • 1
    Actually CreateSubKey will either create it if missing or open it in write mode, so you just need to use create if the situation is indeterminate – Allen May 21 '14 at 21:57
19
if (null == skms)            
{             
   skms = Registry.LocalMachine.OpenSubKey("SOFTWARE",true);              
   RegistryKey key = skms.CreateSubKey(
          RegistryKeyName, RegistryKeyPermissionCheck.ReadWriteSubTree);      
}

This is the answer for my question .

Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
62071072SP
  • 1,963
  • 2
  • 19
  • 38
  • 3
    may I ask why `null` is the first value in your compiration ? is there any meaning to that ? – itsho Sep 29 '12 at 20:12
  • 1
    @itsho, some people prefer to put the 'constant' in a comparison before the variable in the comparision, 'myvar == someconstant' is the same as 'someconstant == myvar', but avoids a mistype of 'myvar = someconstant', which will evaulation to 'true', but will not be the intent of the programmer, and the compiler will not tell you anything is wrong. 'someconstant = myvar' will result in a compiler error on the other hand – Ninjanoel Mar 15 '13 at 16:20
  • 4
    That is not the answer to your question at all. – David Heffernan May 13 '13 at 15:22
  • The second parameter (true) to OpenSubKey seems to be the answer to your question – anakic Mar 17 '20 at 15:50
7

You are probably falling foul of registry redirection. Perhaps you have a 32 bit process on a 64 bit system and writes to HKLM\Software get redirected to HKLM\Software\Wow6432Node.

You need to open the 64 bit key directly, or compile for AnyCPU.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • If I compile for AnyCPU I am getting the following error : The 'Microsoft.Jet.OLEDB.4.0 ' provider is not registered in the local machine – 62071072SP Aug 26 '11 at 10:38
  • Either register and use the 64 Jet, or stay with x86 and open the 64 bit view of registry. – David Heffernan Aug 26 '11 at 11:13
  • For the latter look at the RegistryView enumeration if you target .net 4. Otherwise you have to Pinvoke, believe it or not. – David Heffernan Aug 26 '11 at 11:16
  • @bathineni : Thanks for your reply but i am still getting a problem . I am able to read the registry key if i manually give in wow6432 bit node but not able to create the key through my application . – 62071072SP Aug 31 '11 at 10:36
  • Don't hard code Wow6432node. That is very strongly discouraged by MS. You should use the RegistryView enumeration if you are on .net 4. Otherwise you need to P/invoke. I don't feel inclined to spend much more time on this since you still won't tell us what your configuration is. Are you on 64 bit OS? Is your process 32 or 64 bit? Do you want to write to 32 or 64 bit view of registry? Without those details you are just wasting our time. – David Heffernan Aug 31 '11 at 10:45
  • I am sorry thats my mistake. Application is working fine on 32-bit systems and i am trying to run on 64-bit systems where i am getting error cannot write to the registry key . And i didnt hardcode Wow6432Node i said i hardcoded key in Wow6432 node which i am able to read it and is working fine if i want to create the key through my appln its giving me error .Please see my edited code above for clear stand . – 62071072SP Aug 31 '11 at 11:10
  • Can you tell me the name of the key on a 32 bit system, and the name of the key in a 64 bit system? – David Heffernan Aug 31 '11 at 11:30
  • Name of the key is "Skms" in both 32-bit and 64-bit – 62071072SP Aug 31 '11 at 11:53
  • give me the full paths please – David Heffernan Aug 31 '11 at 11:58
  • HKEY_LOCAL_MACHINE \\SOFTWARE\\Wow6432Node\\Skms FOR 64- BIT – 62071072SP Aug 31 '11 at 12:03
  • HKEY_LOCAL_MACHINE \\SOFTWARE\\Skms for 32-bit – 62071072SP Aug 31 '11 at 12:04
4

try this......someone may find useful....

using System.Security;
using System.Security.AccessControl;
using Microsoft.Win32;

string user = Environment.UserDomainName + "\\" + Environment.UserName;

RegistryKey rk = 
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).
OpenSubKey("SOFTWARE",true);    

RegistrySecurity rs = new RegistrySecurity();

rs.AddAccessRule(new RegistryAccessRule(user,
            RegistryRights.WriteKey | RegistryRights.ReadKey | RegistryRights.Delete,
            InheritanceFlags.None,
            PropagationFlags.None,
            AccessControlType.Allow));
rk = Registry.CurrentUser.CreateSubKey("RegistryRightsExample", 
            RegistryKeyPermissionCheck.Default, rs);
Sukhdevsinh Zala
  • 1,126
  • 2
  • 14
  • 19