I'm trying to add some access rules to registry key to which I've no rights, but I'm an administrator and using "Run as administrator"
command at right mouse button menu.
Unfortunately exception is being thrown (System.UnauthorizedAccessException
).
When running regedit.exe
as administrator I can change rights to this key w/o any problems.
How to add any access rule to this key in my application?
RegistryKey root = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\some_key", RegistryKeyPermissionCheck.ReadWriteSubTree);
RegistrySecurity security = new RegistrySecurity();
SecurityIdentifier sec = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
RegistryAccessRule rule = new RegistryAccessRule(sec, RegistryRights.ReadKey | RegistryRights.QueryValues, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.InheritOnly, AccessControlType.Allow);
security.AddAccessRule(rule);
root.SetAccessControl(security);
root.Close();