RegistryKeyPermissionCheck 枚举

定义

指定在打开注册表项并访问它们的名称/值对时是否执行安全检查。

public enum class RegistryKeyPermissionCheck
public enum RegistryKeyPermissionCheck
type RegistryKeyPermissionCheck = 
Public Enum RegistryKeyPermissionCheck
继承
RegistryKeyPermissionCheck

字段

Default 0

注册表项继承其父项的模式。 除非用 ReadSubTreeReadWriteSubTree 模式打开父项,否则会在尝试访问子项或值时执行安全检查。

ReadSubTree 1

在访问子项或值时,不执行安全检查。 除非用 ReadSubTreeReadWriteSubTree 打开父项,否则会在尝试打开当前项时执行安全检查。

ReadWriteSubTree 2

在访问子项或值时,不执行安全检查。 除非用 ReadWriteSubTree 打开父项,否则会在尝试打开当前项时执行安全检查。

示例

下面的代码示例创建包含 100 个键/值对并关闭它的子项。 此示例使用 Default 打开子项,并记录读取所有值所需的时间。 然后,该示例使用 ReadSubTree 打开子项,并记录读取所有值所需的时间。 最后,示例计算并显示百分比改进。

using System;
using Microsoft.Win32;
using System.Diagnostics;

public class Example
{
    public static void Main()
    {
        const int LIMIT = 100;
        RegistryKey cu = Registry.CurrentUser;
        const string testKey = "RegistryKeyPermissionCheckExample";

        Console.WriteLine("Generating {0} key/value pairs.", LIMIT);
        RegistryKey rk = cu.CreateSubKey(testKey);
        for (int i = 0; i < LIMIT; i++)
        {
            rk.SetValue("Key" + i, i);
        }

        rk.Close();

        Stopwatch s = 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 (int i = 0; i < LIMIT; i++)
        {
            rk.GetValue("Key" + i, i);
        }
        s.Stop();
        rk.Close();
        long delta1 = 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 (int i = 0; i < LIMIT; i++)
        {
            rk.GetValue("Key" + i, i);
        }
        s.Stop();
        rk.Close();
        long delta2 = s.ElapsedTicks;

        double faster = (double) (delta1 - delta2) / (double) delta1;
        Console.WriteLine("ReadSubTree is {0}% faster for {1} values.",
            (faster * 100).ToString("0.0"), LIMIT);

        cu.DeleteSubKey(testKey);
    }
}

/* This code example produces output similar to the following:

Generating 100 key/value pairs.
ReadSubTree is 23.4% faster for 100 values.
 */
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.
'

注解

当应用程序从一组子项保存或检索大量注册表设置时,将执行大量冗余安全检查。 此枚举指定何时省略对密钥进行安全检查。

下表显示何时执行安全检查,具体取决于父密钥和当前密钥打开的方式。

使用 打开的父密钥 打开的当前密钥 结果
默认 默认 访问当前密钥中的任何值或尝试访问子项时,将执行安全检查。 这是.NET Framework版本 1.0 和 1.1 中的行为。
默认 ReadSubTree 尝试打开当前密钥时会执行安全检查。
默认 ReadWriteSubTree 尝试打开当前密钥时会执行安全检查。
ReadSubTree 默认或 ReadSubTree 打开当前密钥或其值时,不会执行任何安全检查。
ReadSubTree ReadWriteSubTree 尝试打开当前密钥时会执行安全检查。
ReadWriteSubTree 任意 打开当前密钥或其值时,不会执行任何安全检查。

适用于