0

I found this code on Stackoverflow and used it in a Outlook 2010 add-in and for some reason I still get

"Requested registry access is not allowed."

I am trying to change a value in a DWORD key.

var admin = WindowsIdentity.GetCurrent().Name;
            RegistryKey LocalMachine = Registry.LocalMachine;
            RegistryKey rk = LocalMachine.OpenSubKey(sSubKey, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.ChangePermissions | RegistryRights.ReadKey);//Get the registry key desired with ChangePermissions Rights.
           if (rk == null)
            {
                System.Windows.Forms.MessageBox.Show("No RegKey");
            }
            RegistrySecurity rs = new RegistrySecurity();
            rs.AddAccessRule(new RegistryAccessRule(admin, RegistryRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));//Create access rule giving full control to the Administrator user.
            rk.SetAccessControl(rs); //Apply the new access rule to this Registry Key.
            rk = Registry.LocalMachine.OpenSubKey(sKSubKey, Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl); // Opens the key again with full control.
            rs.SetOwner(new NTAccount(admin));// Set the securitys owner to be Administrator
            rk.SetAccessControl(rs);// Set the key with 

            Registry.SetValue(sSubKey, sKey, sValue, RegistryValueKind.DWord);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Ren-Heok
  • 67
  • 1
  • 1
  • 11

2 Answers2

1

You can modify the HKLM hive only if your code is running as a local admin and UAC is either off or your app had been grated elevated privileges.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

You can't write to the HKLM hive in newer versions of Windows (starting from Vista) unless you have administrative privileges. Take a look at the similar threads for more information:

Community
  • 1
  • 1
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45