I'm trying to add a value to this windows key while running elevated (Administrator):
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing
By default, Administrator only has read permissions to this key. I was thinking of using AddAccessRule() to add myself to that key temporarily, but I cannot do this because in order to use AddAccessRule(), I first need to open the key with "write" access, hence I'm caught in a loop here. These are the current permissions:
This is the code that doesn't work, obviously, because OpenSubkey() fails.
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing", true))
{
var current = key.GetAccessControl();
var user = ...;
current.AddAccessRule(new RegistryAccessRule(user, RegistryRights.WriteKey, AccessControlType.Allow));
key.SetAccessControl(current);
key.SetValue("TestValue", 1);
}
How should I work myself into that key, so I can write a value there?