This documentation is archived and is not being maintained.

RegistryKeyPermissionCheck Enumeration

Specifies whether security checks are performed when opening registry keys and accessing their name/value pairs.

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

'Declaration
Public Enumeration RegistryKeyPermissionCheck
'Usage
Dim instance As RegistryKeyPermissionCheck

Member nameDescription
Supported by the .NET Compact FrameworkDefaultThe registry key inherits the mode of its parent. Security checks are performed when trying to access subkeys or values, unless the parent was opened with ReadSubTree or ReadWriteSubTree mode.
Supported by the .NET Compact FrameworkReadSubTreeSecurity checks are not performed when accessing subkeys or values. A security check is performed when trying to open the current key, unless the parent was opened with ReadSubTree or ReadWriteSubTree.
Supported by the .NET Compact FrameworkReadWriteSubTreeSecurity checks are not performed when accessing subkeys or values. A security check is performed when trying to open the current key, unless the parent was opened with ReadWriteSubTree.

When an application saves or retrieves a large number of registry settings from a set of subkeys, numerous redundant security checks are performed. This enumeration specifies when security checks on a key are to be omitted.

The following table shows when security checks are performed, based on the way the parent key and the current key are opened.

Parent key opened with

Current key opened with

Result

Default

Default

A security check is performed when accessing any value in the current key, or when attempting to access a subkey. This is the behavior in the .NET Framework versions 1.0 and 1.1.

Default

ReadSubTree

A security check is performed when trying to open the current key.

Default

ReadWriteSubTree

A security check is performed when trying to open the current key.

ReadSubTree

Default or ReadSubTree

No security checks are performed when opening the current key or its values.

ReadSubTree

ReadWriteSubTree

A security check is performed when trying to open the current key.

ReadWriteSubTree

Any

No security checks are performed when opening the current key or its values.

The following code example creates a subkey containing 100 key/value pairs and closes it. The example opens the subkey with Default and records the time it takes to read all the values. Then the example opens the subkey with ReadSubTree and records the time it takes to read all the values. Finally, the example computes and displays the percentage improvement.

Imports System
Imports Microsoft.Win32
Imports System.Diagnostics

Public Class Example

    Public Shared Sub Main() 

        Const LIMIT As Integer = 100
        Dim cu As RegistryKey = Registry.CurrentUser
        Const testKey As String = "RegistryKeyPermissionCheckExample"

        Console.WriteLine("Generating {0} key/value pairs.", LIMIT)
        Dim rk As RegistryKey = cu.CreateSubKey(testKey)

        For i As Integer = 0 To LIMIT
            rk.SetValue("Key" & i, i)
        Next i

        rk.Close()

        Dim s As New Stopwatch()

        ' On the default setting, security is checked every time 
        ' a key/value pair is read.
        rk = cu.OpenSubKey(testKey, _
            RegistryKeyPermissionCheck.Default)

        s.Start()
        For i As Integer = 0 To LIMIT
            rk.GetValue("Key" & i, i)
        Next i
        s.Stop()
        rk.Close()
        Dim delta1 As Long = s.ElapsedTicks

        s.Reset()

        ' When the key is opened with ReadSubTree, security is  
        ' not checked when the values are read.
        rk = cu.OpenSubKey(testKey, _
            RegistryKeyPermissionCheck.ReadSubTree)

        s.Start()
        For i As Integer = 0 To LIMIT
            rk.GetValue("Key" & i, i)
        Next i
        s.Stop()
        rk.Close()
        Dim delta2 As Long = s.ElapsedTicks

        Dim faster As Double = _
            CDbl(delta1 - delta2) * 100.0 / CDbl(delta1)
        Console.WriteLine("ReadSubTree is {0}% faster for {1} values.", _
            faster.ToString("0.0"), LIMIT)

        cu.DeleteSubKey(testKey)

    End Sub  
End Class  

' This code example produces output similar to the following: 

'Generating 100 key/value pairs. 
'ReadSubTree is 23.4% faster for 100 values. 
' 

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0
Show: