This documentation is archived and is not being maintained.

RegistryKey.SetAccessControl Method

Applies Windows access control security to an existing registry key.

Namespace:  Microsoft.Win32
Assembly:  mscorlib (in mscorlib.dll)

member SetAccessControl : 
        registrySecurity:RegistrySecurity -> unit 

Parameters

registrySecurity
Type: System.Security.AccessControl.RegistrySecurity
The access control security to apply to the current subkey.

ExceptionCondition
UnauthorizedAccessException

The current RegistryKey object represents a key with access control security, and the caller does not have RegistryRights.ChangePermissions rights.

ArgumentNullException

registrySecurity is a null reference (Nothing in Visual Basic).

ObjectDisposedException

The RegistryKey being manipulated is closed (closed keys cannot be accessed).

To modify permissions for a registry key, use the GetAccessControl method to obtain a RegistrySecurity object representing the existing Windows access control security, modify that RegistrySecurity object, and then use the SetAccessControl method to update security for the key.

Caution noteCaution

The RegistrySecurity object specified for registrySecurity replaces the existing security for the registry key. To add permissions for a new user, use the GetAccessControl method to obtain the existing access control security, and then modify it.

The following code example creates a test key. The current user is allowed ReadKey and Delete rights but denied ChangePermissions and WriteKey rights. Subsequent attempts to manipulate the key succeed or fail depending on these permissions.

Before the key is deleted, the code pauses. You can switch to the registry editor and verify that the same access rights apply when the key is accessed using the registry editor. (This works best if you use RunAs from the command line to run the registry editor and the sample code as a local user without administrator rights. The registry editor always allows an administrator to change permissions, even if the particular administrator has been denied those rights. If you have defined a local user named TestUser, the command runas /user:TestUser cmd opens a command window from which you can run the registry editor and then the sample code.)

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: