Search code examples
c#registry

In Visual Studio: Trying to delete registry entry but "Requested registry access is not allowed"


I have searched for quite a while on a solution to this. At least I think I understand the problem, but I have yet to come to any solution.

What I need is either some sort of executable or a script that will delete some registry entries. The problem is that the registry entries in question only give read/write access to SYSTEM and no one else. The only way that I can delete them is by going into regedit, setting myself as the owner, and finally setting full control to everyone. Only then can the keys be deleted. I need this process to be in some sort of script though!

So in C#, I first make sure that the software has administrative rights. Then I try to execute the following.

    RegistryKey reg_localmachine = RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, "");
    RegistryKey key = reg_localmachine.OpenSubKey(@"SYSTEM\path to subkey", true);

On the second line, when I try to access the sub key with write access, I get the exception "Requested registry access is not allowed." In order for me to change owner or grant permissions, I need to execute SetAccessControl() on the RegistryKey. In order to set access control, I need write privileges for the key. So I am in this paradox.


Solution

  • Security is there for a reason.

    You don't want any old program to be able to come in and start hacking around in the registry.

    Each entry in the registry has it's own set of DACLS. There are only two solutions:-

    1. Change the account under which your program is running to an account that has permission to delete the registry entry.

    2. Change the DACLS on the registry entry to include the account your program is running under.