Programmatically Assign Permission to a Registry Key

Here's how we manuallyassign permissions to a registry key:

Assign Permissions to a Registry Key

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

  • From the Edit menu, select Permissions.

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

  • To give the user the right to read the contents of the key, but not save any changes made to the file, in the Permissions for name section, for Read , select the Allow check box.

  • To grant the user the right to open, edit and own the selected key in the "Permissions for the name" section for Full control , select the Allow check box.

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

So my question is: is it possible to do this programmatically? Say, if I want to give users full control for a specific section, how can I write code in C #? Many thanks.

+5
source share
3 answers

RegistrySecuritythe class is also useful here. Therefore, we can write the following code to apply the access rules to the registry key for the 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));
+3
source
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 access rights to the specified user to the registry key "key"

+4
source

This is about the RegSetKeySecurityAPI that is associated with .NET code through RegistryKey.SetAccessControl, see Using RegSetKeySecurity to Avoid Registry Redirection

+1
source

All Articles