Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically assign the permission to a registry subkey

Here is how we manually assign the permissions to a registry key:

To assign permissions to a registry key

  • Open Registry Editor. Click the key to which you want to assign permissions.

  • On the Edit menu, click Permissions.

  • Assign an access level to the selected key as follows:

  • To grant the user permission to read the key contents, but not save any changes made to the file, under Permissions for name, for Read, select the Allow check box.

  • To grant the user permission to open, edit, and take ownership of the selected key, under Permissions for name, for Full Control, select the Allow check box.

  • To grant the user special permission in the selected key, click Advanced.

So my question is, would it be possible to do it programmatically? Say, if I want to grant Users full control permission on a particular subkey, how should I write the code in C#? Thanks very much.

like image 250
woodykiddy Avatar asked Oct 28 '11 09:10

woodykiddy


People also ask

How do I change permissions on my registry?

In Registry Editor, right-click the key that you can't edit (or the key that contains the value you can't edit) and then choose “Permissions” from the context menu. In the Permissions window that appears, click the “Advanced” button. Next, you're going to take ownership of the Registry key.

How do I change registry permissions in CMD?

To change a registry value or registry permissions from a command line or from a script, use the Regini.exe utility. The Regini.exe utility is included in the Windows NT Server 4.0 Resource Kit, in the Microsoft Windows 2000 Resource Kit, and in the Microsoft Windows Server 2003 Resource Kit.

What is a subkey in regedit?

This identifies the start of a new registry path. Each key or subkey is a new registry path. If you have several keys in your . reg file, blank lines can help you to examine and to troubleshoot the contents.


2 Answers

Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.LocalMachine;
RegistrySecurity rs = new RegistrySecurity();
rs = key.GetAccessControl();
string currentUserStr = Environment.UserDomainName + "\\" + Environment.UserName;
rs.AddAccessRule(
    new RegistryAccessRule(
        currentUserStr, 
        RegistryRights.WriteKey 
        | RegistryRights.ReadKey 
        | RegistryRights.Delete 
        | RegistryRights.FullControl, 
        AccessControlType.Allow));

This will assign the access rights to the specified user to the registry key 'key'

like image 110
Ajay Bhasy Avatar answered Sep 18 '22 19:09

Ajay Bhasy


RegistrySecurity class is also useful here. So we can write the following code to apply access rules on the registry key for a current user.

RegistrySecurity rs = new RegistrySecurity(); // it is right string for this code
string currentUserStr = Environment.UserDomainName + "\\" + Environment.UserName;
rs.AddAccessRule(new RegistryAccessRule(currentUserStr, RegistryRights.WriteKey | RegistryRights.ReadKey | RegistryRights.Delete | RegistryRights.FullControl, AccessControlType.Allow));
like image 22
woodykiddy Avatar answered Sep 20 '22 19:09

woodykiddy