Provides support for managing access and audit control lists for Microsoft.Win32.RegistryKey. Commonly Used Types: System.Security.AccessControl.RegistryAccessRule System.Security.AccessControl.RegistryAuditRule System.Security.AccessControl.RegistrySecurity
$ dotnet add package Microsoft.Win32.Registry.AccessControlProvides support for managing access control lists for Microsoft.Win32.RegistryKey.
using Microsoft.Win32;
using System.Security.AccessControl;
// Open a registry key (or create it if it doesn't exist)
using RegistryKey registryKey = Registry.CurrentUser.CreateSubKey("TestKey");
if (registryKey == null)
{
Console.WriteLine("Failed to create or open the registry key.");
return;
}
// Get the current access control list (ACL) for the registry key
RegistrySecurity registrySecurity = registryKey.GetAccessControl();
Console.WriteLine("Current Access Control List (ACL):");
Console.WriteLine(registrySecurity.GetSecurityDescriptorSddlForm(AccessControlSections.Access));
// Create a new access rule granting full control to the current user
string currentUser = Environment.UserName;
RegistryAccessRule accessRule = new RegistryAccessRule(currentUser, RegistryRights.FullControl, InheritanceFlags.None, PropagationFlags.None, AccessControlType.Allow);
// Add the new access rule to the ACL
registrySecurity.AddAccessRule(accessRule);
// Set the updated ACL on the registry key
registryKey.SetAccessControl(registrySecurity);
// Get and display the updated ACL for the registry key using the second GetAccessControl overload
RegistrySecurity updatedRegistrySecurity = registryKey.GetAccessControl(AccessControlSections.Access);
Console.WriteLine("Updated Access Control List (ACL):");
Console.WriteLine(updatedRegistrySecurity.GetSecurityDescriptorSddlForm(AccessControlSections.Access));
The main type provided by this library is:
Microsoft.Win32.RegistryAclExtensionsMicrosoft.Win32.Registry.AccessControl is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.