cannot write to the registry key

前端 未结 4 2297
耶瑟儿~
耶瑟儿~ 2020-12-05 13:25

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 Admin

相关标签:
4条回答
  • 2020-12-05 13:38

    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);
    
    0 讨论(0)
  • 2020-12-05 13:42
    if (null == skms)            
    {             
       skms = Registry.LocalMachine.OpenSubKey("SOFTWARE",true);              
       RegistryKey key = skms.CreateSubKey(
              RegistryKeyName, RegistryKeyPermissionCheck.ReadWriteSubTree);      
    }
    

    This is the answer for my question .

    0 讨论(0)
  • 2020-12-05 13:49

    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");
    
    0 讨论(0)
  • 2020-12-05 13:49

    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.

    0 讨论(0)
提交回复
热议问题