3
Vote

How do I take ownership of a Registry Key using C#?

description

have a registry key whose owner is "Trusted Installer".
 

 
Now I know I can take ownership of the key manually by using registry editor by doing these steps:
 
  1. Right-click the key - Click Permissions - Click Advanced
     
  2. Click Owner Tab, Click your user name and check the box "Replace owner on subcontainers and objects", Click Apply - Click OK.
     
  3. Click Administrators under Group or user name, check Allow Full Control. Then Click Apply - Click OK.
     
 
Now my question is, How can I take ownership of the registry key programmatically?
 
If I am able to take ownership of a registry key programmatically, then my problem is solved. Please help.
 
I've tried doing this:
 
string user = Environment.UserDomainName + "\" + Environment.UserName;
RegistrySecurity rs = new RegistrySecurity();
rs.AddAccessRule(new RegistryAccessRule(user,
       RegistryRights.ReadKey | RegistryRights.Delete,
       InheritanceFlags.None,
       PropagationFlags.None,
       AccessControlType.Allow));
rs.AddAccessRule(new RegistryAccessRule(user,
       RegistryRights.WriteKey | RegistryRights.ChangePermissions,
       InheritanceFlags.None,
       PropagationFlags.None,
       AccessControlType.Allow));
 
RegistryKey RK = Registry.ClassesRoot.OpenSubKey("<somekey>", true);
RK.SetAccessControl(rs);
 
 
but an error message that says that "Requested registry access is not allowed" and it occurs on this line:
 
RegistryKey RK = Registry.ClassesRoot.OpenSubKey("<somekey>", true);<br/>
 
I am also running Visual Studio as Administrator.
 

 
Again, If you know how to programmatically take ownership of a registry key, then you have solved my problem.
 
I understand you can also use these functions: RegOpenKeyEx and RegSetKeySecurity to Take Ownership of a registry key but I don't know how to use them. Can someone give a sample with an explanation on how to use them.
 
I have seen these sites before:
http://support.microsoft.com/kb/111546/EN-US/
and
http://msdn.microsoft.com/en-us/library/ms724897%28VS.85%29.aspx
 
But I still don't understand how I can use them in C#.
 
Thanks in advance.

comments

orichisonic wrote May 5, 2011 at 9:26 AM

Hello anarki,

Thank you for using our code sample request service.

I'd like to confirm with you my understanding of the sample request:

Your sample idea is that you want to use RegOpenKeyEx and RegSetKeySecurity to realize how to take ownership of a registry key. It seems that these features can be achieved via AccessControl namespace in VS2010 environment. But I think C++ language has better ability to complete it because of GetTokenInformation is only included in win32 functions. And I tell you a good news I have completed it in my previous sample. Never mind. It would be published when it is approved by our subject matter experts.



Is my understanding correct? Please let me know if I misunderstood your needs.


Thanks
All-In-One Code Framework
http://1code.codeplex.com

iCodeRegistry wrote May 9, 2011 at 10:50 AM

yes your understanding is correct. Can I see this code/sample that you speak of?

To make it more clearer, here's what I want to happen:
After running the program I want the owner of a specific key to be me. NOT TrustedInstaller.

For example,
I have this key:
HKEY_CLASSES_ROOT\CLSID{f81b1b56-7613-4ee4-bc05-1fab5de5c07e}

when I right click on "{f81b1b56-7613-4ee4-bc05-1fab5de5c07e}"
then click on Permissions
then click on Advanced
then click on Owner Tab
I would see my username in the Current Owner textbox instead of TrustedInstaller.

That's what I want to happen after running the program.

Is that possible?

Thank you very much.

orichisonic wrote May 11, 2011 at 2:12 AM

Hello iCodeRegistry ,
I know your meaning at my landscope. I think this sample should get Token Information and impersonate it if you have not the administrator or owner privilege. It seems that it is worth to be composed. I will concern on its vote until it reaches enough point. Thanks for providing valuable suggestion.

Welcome you continue to support our project.



Thanks
All-In-One Code Framework
http://1code.codeplex.com

orichisonic wrote May 11, 2011 at 2:36 AM

Hello iCodeRegistry ,
I am sorry that I don't make clear your content. You sample idea should use SetOwner function to do that and it should invoke NTAccount class which contains in System.Security.Principal namespace. And if you want to get the group's owner, then you should use GetGroup function.
I am sorry again about my careless. Thanks for providing valuable suggestion.



Welcome you continue to support our project.



Thanks
All-In-One Code Framework
http://1code.codeplex.com

wrote Jun 28, 2012 at 6:26 AM

Serpox wrote Jun 28, 2012 at 6:28 AM

I am looking for the same solution to take registry ownership via C# code. Any word when the code is going to be released.

wrote Feb 22, 2013 at 2:00 AM

wrote Oct 26, 2014 at 12:00 AM

FAIZ99 wrote Oct 26, 2014 at 12:14 AM

WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
if(principal.IsInRole(WindowsBuiltInRole.Administrator) ? "true" : "false")
      Console.WriteLine("Yes i am an admin");
else
      Console.WriteLine("No i am not an admin");
using (RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"your_Subkey_ Path", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.ChangePermissions))
{
var rSec = new RegistrySecurity();
rSec .SetGroup(new NTAccount("Administrators"));
rSec .SetOwner(new NTAccount("Administrators"));
rSec .SetAccessRuleProtection(false, false);
regKey.SetAccessControl(rSec );
}
Console.ReadLine();


try this if it works for you