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);