How do you change administrator rights?

Add registry access rule to key to which I've no access but I'm an administrator

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

  • Answer:

    I think you should use the GetAccessControl() method to get a RegistrySecurity object: RegistryKey root = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\some_key", RegistryKeyPermissionCheck.ReadWriteSubTree); RegistrySecurity security = root.GetAccessControl(); You probably get the exception because you are trying to overwrite the security rules, instead of modifying them.

daftu at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

The http://msdn.microsoft.com/en-us/library/microsoft.win32.registrykey.setaccesscontrol.aspx is pretty clear: UnauthorizedAccessException: The current RegistryKey object represents a key with access control security, and the caller does not have RegistryRights.ChangePermissions rights. See also the documentation for http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.registryrights%28v=VS.100%29.aspx.

Jon

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.