Replacing or Taking Registry ownership programmatically

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
<pre lang="x-c# try
{
using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesBITS", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.ChangePermissions))
{
RegistrySecurity rs = regKey.GetAccessControl(AccessControlSections.Access);
rs.SetGroup(new NTAccount("Administrators"));
rs.SetOwner(new NTAccount("Administrators"));
rs.SetAccessRuleProtection(false, false);

regKey.SetAccessControl(rs);
}

MessageBox.Show("Permission set successfully.");
}
catch (Exception)
{
throw;
}[/code]
This may be an ubiquitous question but still I am not able to resolve in fully in manage (and some unmanged/PInvoke methods) solution.
The problem was, I couldnt took the registry/file object ownership when the current owner is NULL or "Unable to display the current owner." even I embedded the manifest (requestedExecutionLevel level=requireAdministrator); I tried testing these on
WinXPSP3, Vista and Win7 host machines.
The infamous "Requested registry access is not allowed" and "Attempted to perform an unauthorized operation" from SecurityException class and UnauthorizedAccessException respectively will be shown even I got full access I mean Administrator account.
I could manually took the ownership via registry editor and subinacl.exe tool from microsoft. How could I implement in purely C# code?

View the full article
 
Back
Top