Jazyk

RegistryKey.OpenSubKey Metoda

Definice

Načte zadaný podklíč.

Přetížení

Name Description
OpenSubKey(String, Boolean)

Načte zadaný podklíč a určuje, jestli se má pro klíč použít přístup k zápisu.

OpenSubKey(String, RegistryKeyPermissionCheck, RegistryRights)

Načte zadaný podklíč pro přístup pro čtení nebo čtení/zápis a požaduje zadaná přístupová práva.

OpenSubKey(String, RegistryRights)

Načte podklíč se zadaným názvem a přístupovými právy. K dispozici od .NET Framework 4.6.

OpenSubKey(String)

Načte podklíč jen pro čtení.

OpenSubKey(String, RegistryKeyPermissionCheck)

Načte zadaný podklíč pro přístup pro čtení nebo čtení a zápis.

OpenSubKey(String, Boolean)

Zdroj:
RegistryKey.cs

Načte zadaný podklíč a určuje, jestli se má pro klíč použít přístup k zápisu.

public:
 Microsoft::Win32::RegistryKey ^ OpenSubKey(System::String ^ name, bool writable);
public Microsoft.Win32.RegistryKey? OpenSubKey(string name, bool writable);
public Microsoft.Win32.RegistryKey OpenSubKey(string name, bool writable);
member this.OpenSubKey : string * bool -> Microsoft.Win32.RegistryKey
Public Function OpenSubKey (name As String, writable As Boolean) As RegistryKey

Parametry

name
String

Název nebo cesta podklíče, který se má otevřít.

writable
Boolean

Nastavte, true pokud potřebujete přístup k klíči pro zápis.

Návraty

Požadovaný podklíč nebo null pokud operace selhala.

Výjimky

name je null.

Je RegistryKey zavřený (uzavřené klíče nelze získat přístup).

Uživatel nemá oprávnění požadovaná pro přístup ke klíči registru v zadaném režimu.

Příklady

Následující příklad kódu vytvoří testovací klíč a použije metodu OpenSubKey k otevření a demonstruje obě přetížení metody.

#using <Microsoft.VisualBasic.dll>

using namespace System;
using namespace Microsoft::Win32;
using namespace Microsoft::VisualBasic;

int main()
{
    // Delete and recreate the test key.
    Registry::CurrentUser->DeleteSubKey( L"RegistryOpenSubKeyExample", false );
    RegistryKey ^ rk = Registry::CurrentUser->CreateSubKey( L"RegistryOpenSubKeyExample" );
    rk->Close();

    // Obtain an instance of RegistryKey for the CurrentUser registry
    // root.
    RegistryKey ^ rkCurrentUser = Registry::CurrentUser;

    // Obtain the test key (read-only) and display it.
    RegistryKey ^ rkTest = rkCurrentUser->OpenSubKey( L"RegistryOpenSubKeyExample" );
    Console::WriteLine( L"Test key: {0}", rkTest );
    rkTest->Close();
    rkCurrentUser->Close();

    // Obtain the test key in one step, using the CurrentUser registry
    // root.
    rkTest = Registry::CurrentUser->OpenSubKey( L"RegistryOpenSubKeyExample" );
    Console::WriteLine( L"Test key: {0}", rkTest );
    rkTest->Close();

    // Open the test key in read/write mode.
    rkTest = Registry::CurrentUser->OpenSubKey( L"RegistryOpenSubKeyExample", true );
    rkTest->SetValue( L"TestName", L"TestValue" );
    Console::WriteLine( L"Test value for TestName: {0}", rkTest->GetValue( L"TestName" ) );
    rkTest->Close();

    return 0;
} //Main
using System;
using Microsoft.Win32;
using Microsoft.VisualBasic;

public class Example
{
    public static void Main()
    {
        // Delete and recreate the test key.
        Registry.CurrentUser.DeleteSubKey("RegistryOpenSubKeyExample", false);
        RegistryKey rk = Registry.CurrentUser.CreateSubKey("RegistryOpenSubKeyExample");
        rk.Close();

        // Obtain an instance of RegistryKey for the CurrentUser registry
        // root.
        RegistryKey rkCurrentUser = Registry.CurrentUser;

        // Obtain the test key (read-only) and display it.
        RegistryKey rkTest = rkCurrentUser.OpenSubKey("RegistryOpenSubKeyExample");
        Console.WriteLine("Test key: {0}", rkTest);
        rkTest.Close();
        rkCurrentUser.Close();

        // Obtain the test key in one step, using the CurrentUser registry
        // root.
        rkTest = Registry.CurrentUser.OpenSubKey("RegistryOpenSubKeyExample");
        Console.WriteLine("Test key: {0}", rkTest);
        rkTest.Close();

        // Open the test key in read/write mode.
        rkTest = Registry.CurrentUser.OpenSubKey("RegistryOpenSubKeyExample", true);
        rkTest.SetValue("TestName", "TestValue");
        Console.WriteLine("Test value for TestName: {0}", rkTest.GetValue("TestName"));
        rkTest.Close();
    } //Main
} //Example
Imports Microsoft.Win32

Public Class Example
    Public Shared Sub Main()
        ' Delete and recreate the test key.
        Registry.CurrentUser.DeleteSubKey("RegistryOpenSubKeyExample", False)
        Dim rk As RegistryKey = Registry.CurrentUser.CreateSubKey("RegistryOpenSubKeyExample")
        rk.Close

        ' Obtain an instance of RegistryKey for the CurrentUser registry 
        ' root. 
        Dim rkCurrentUser As RegistryKey = Registry.CurrentUser

        ' Obtain the test key (read-only) and display it.
        Dim rkTest As RegistryKey = rkCurrentUser.OpenSubKey("RegistryOpenSubKeyExample")
        Console.WriteLine("Test key: {0}", rkTest)
        rkTest.Close
        rkCurrentUser.Close

        ' Obtain the test key in one step, using the CurrentUser registry 
        ' root.
        rkTest = Registry.CurrentUser.OpenSubKey("RegistryOpenSubKeyExample")
        Console.WriteLine("Test key: {0}", rkTest)
        rkTest.Close

        ' Obtain the test key in read/write mode.
        rkTest = Registry.CurrentUser.OpenSubKey("RegistryOpenSubKeyExample", True)
        rkTest.SetValue("TestName", "TestValue")
        Console.WriteLine("Test value for TestName: {0}", rkTest.GetValue("TestName"))
        rkTest.Close
    End Sub
End Class

Poznámky

Pokud požadovaný klíč neexistuje, vrátí tato metoda null místo vyvolání výjimky.

Pokud writable ano true, klíč se otevře pro čtení a zápis, jinak se klíč otevře jako jen pro čtení.

Chcete-li použít metodu OpenSubKey , musíte mít instanci RegistryKey metody. Pokud chcete získat instanci RegistryKey, použijte jeden ze statických Registry členů třídy.

Viz také

Platí pro

OpenSubKey(String, RegistryKeyPermissionCheck, RegistryRights)

Zdroj:
RegistryKey.cs

Načte zadaný podklíč pro přístup pro čtení nebo čtení/zápis a požaduje zadaná přístupová práva.

public:
 Microsoft::Win32::RegistryKey ^ OpenSubKey(System::String ^ name, Microsoft::Win32::RegistryKeyPermissionCheck permissionCheck, System::Security::AccessControl::RegistryRights rights);
public Microsoft.Win32.RegistryKey? OpenSubKey(string name, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, System.Security.AccessControl.RegistryRights rights);
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey OpenSubKey(string name, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, System.Security.AccessControl.RegistryRights rights);
public Microsoft.Win32.RegistryKey OpenSubKey(string name, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck, System.Security.AccessControl.RegistryRights rights);
member this.OpenSubKey : string * Microsoft.Win32.RegistryKeyPermissionCheck * System.Security.AccessControl.RegistryRights -> Microsoft.Win32.RegistryKey
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.OpenSubKey : string * Microsoft.Win32.RegistryKeyPermissionCheck * System.Security.AccessControl.RegistryRights -> Microsoft.Win32.RegistryKey
Public Function OpenSubKey (name As String, permissionCheck As RegistryKeyPermissionCheck, rights As RegistryRights) As RegistryKey

Parametry

name
String

Název nebo cesta podklíče k vytvoření nebo otevření.

permissionCheck
RegistryKeyPermissionCheck

Jedna z hodnot výčtu, která určuje, zda je klíč otevřen pro přístup pro čtení nebo čtení/zápis.

rights
RegistryRights

Bitová kombinace hodnot výčtu, která určuje požadovaný přístup zabezpečení.

Návraty

Požadovaný podklíč nebo null pokud operace selhala.

Atributy

Výjimky

name je null

permissionCheck obsahuje neplatnou hodnotu.

Je RegistryKey zavřený (uzavřené klíče nelze získat přístup).

rights obsahuje neplatné hodnoty práv registru.

nebo

Uživatel nemá požadovaná oprávnění.

Poznámky

Místo vyvolání výjimky tato metoda vrátí null , pokud požadovaný klíč neexistuje.

Pokud permissionCheck je RegistryKeyPermissionCheck.ReadWriteSubTree, klíč je otevřen pro čtení a zápis; pokud permissionCheck je RegistryKeyPermissionCheck.ReadSubTree nebo RegistryKeyPermissionCheck.Default, klíč je otevřen pro čtení, pokud nebyl otevřen nadřazený klíč s RegistryKeyPermissionCheck.ReadWriteSubTree.

Přístup zadaný pro permissionCheck má přednost před přístupem určeným pro rights. Pokud například zadáte RegistryKeyPermissionCheck.ReadSubTree pro permissionCheck a RegistryRights.WriteKey pro rights, pokus o zápis do podklíče vyvolá výjimku.

Chcete-li použít metodu OpenSubKey , musíte mít instanci RegistryKey třídy. Pokud chcete získat instanci RegistryKey, použijte jeden ze statických Registry členů třídy.

Viz také

Platí pro

OpenSubKey(String, RegistryRights)

Zdroj:
RegistryKey.cs

Načte podklíč se zadaným názvem a přístupovými právy. K dispozici od .NET Framework 4.6.

public:
 Microsoft::Win32::RegistryKey ^ OpenSubKey(System::String ^ name, System::Security::AccessControl::RegistryRights rights);
public Microsoft.Win32.RegistryKey? OpenSubKey(string name, System.Security.AccessControl.RegistryRights rights);
public Microsoft.Win32.RegistryKey OpenSubKey(string name, System.Security.AccessControl.RegistryRights rights);
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey OpenSubKey(string name, System.Security.AccessControl.RegistryRights rights);
member this.OpenSubKey : string * System.Security.AccessControl.RegistryRights -> Microsoft.Win32.RegistryKey
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.OpenSubKey : string * System.Security.AccessControl.RegistryRights -> Microsoft.Win32.RegistryKey
Public Function OpenSubKey (name As String, rights As RegistryRights) As RegistryKey

Parametry

name
String

Název nebo cesta podklíče k vytvoření nebo otevření.

rights
RegistryRights

Práva pro klíč registru.

Návraty

Požadovaný podklíč nebo null pokud operace selhala.

Atributy

Výjimky

name je null.

Je RegistryKey zavřený (uzavřené klíče nelze získat přístup).

Uživatel nemá oprávnění požadovaná pro přístup ke klíči registru v zadaném režimu.

Poznámky

Abyste mohli pracovat s jinými metodami a vlastnostmi, musíte klíč otevřít. Chcete-li upravit klíč, musíte jej otevřít s přetížením OpenSubKey metody, která umožňuje zadat přístup k zápisu.

Platí pro

OpenSubKey(String)

Zdroj:
RegistryKey.cs

Načte podklíč jen pro čtení.

public:
 Microsoft::Win32::RegistryKey ^ OpenSubKey(System::String ^ name);
public Microsoft.Win32.RegistryKey? OpenSubKey(string name);
public Microsoft.Win32.RegistryKey OpenSubKey(string name);
member this.OpenSubKey : string -> Microsoft.Win32.RegistryKey
Public Function OpenSubKey (name As String) As RegistryKey

Parametry

name
String

Název nebo cesta podklíče, který se má otevřít jen pro čtení.

Návraty

Požadovaný podklíč nebo null pokud operace selhala.

Výjimky

name je null

Je RegistryKey zavřený (uzavřené klíče nelze získat přístup).

Uživatel nemá oprávnění potřebná ke čtení klíče registru.

Příklady

Následující příklad kódu vytvoří testovací klíč a použije metodu OpenSubKey k otevření a demonstruje obě přetížení metody.

#using <Microsoft.VisualBasic.dll>

using namespace System;
using namespace Microsoft::Win32;
using namespace Microsoft::VisualBasic;

int main()
{
    // Delete and recreate the test key.
    Registry::CurrentUser->DeleteSubKey( L"RegistryOpenSubKeyExample", false );
    RegistryKey ^ rk = Registry::CurrentUser->CreateSubKey( L"RegistryOpenSubKeyExample" );
    rk->Close();

    // Obtain an instance of RegistryKey for the CurrentUser registry
    // root.
    RegistryKey ^ rkCurrentUser = Registry::CurrentUser;

    // Obtain the test key (read-only) and display it.
    RegistryKey ^ rkTest = rkCurrentUser->OpenSubKey( L"RegistryOpenSubKeyExample" );
    Console::WriteLine( L"Test key: {0}", rkTest );
    rkTest->Close();
    rkCurrentUser->Close();

    // Obtain the test key in one step, using the CurrentUser registry
    // root.
    rkTest = Registry::CurrentUser->OpenSubKey( L"RegistryOpenSubKeyExample" );
    Console::WriteLine( L"Test key: {0}", rkTest );
    rkTest->Close();

    // Open the test key in read/write mode.
    rkTest = Registry::CurrentUser->OpenSubKey( L"RegistryOpenSubKeyExample", true );
    rkTest->SetValue( L"TestName", L"TestValue" );
    Console::WriteLine( L"Test value for TestName: {0}", rkTest->GetValue( L"TestName" ) );
    rkTest->Close();

    return 0;
} //Main
using System;
using Microsoft.Win32;
using Microsoft.VisualBasic;

public class Example
{
    public static void Main()
    {
        // Delete and recreate the test key.
        Registry.CurrentUser.DeleteSubKey("RegistryOpenSubKeyExample", false);
        RegistryKey rk = Registry.CurrentUser.CreateSubKey("RegistryOpenSubKeyExample");
        rk.Close();

        // Obtain an instance of RegistryKey for the CurrentUser registry
        // root.
        RegistryKey rkCurrentUser = Registry.CurrentUser;

        // Obtain the test key (read-only) and display it.
        RegistryKey rkTest = rkCurrentUser.OpenSubKey("RegistryOpenSubKeyExample");
        Console.WriteLine("Test key: {0}", rkTest);
        rkTest.Close();
        rkCurrentUser.Close();

        // Obtain the test key in one step, using the CurrentUser registry
        // root.
        rkTest = Registry.CurrentUser.OpenSubKey("RegistryOpenSubKeyExample");
        Console.WriteLine("Test key: {0}", rkTest);
        rkTest.Close();

        // Open the test key in read/write mode.
        rkTest = Registry.CurrentUser.OpenSubKey("RegistryOpenSubKeyExample", true);
        rkTest.SetValue("TestName", "TestValue");
        Console.WriteLine("Test value for TestName: {0}", rkTest.GetValue("TestName"));
        rkTest.Close();
    } //Main
} //Example
Imports Microsoft.Win32

Public Class Example
    Public Shared Sub Main()
        ' Delete and recreate the test key.
        Registry.CurrentUser.DeleteSubKey("RegistryOpenSubKeyExample", False)
        Dim rk As RegistryKey = Registry.CurrentUser.CreateSubKey("RegistryOpenSubKeyExample")
        rk.Close

        ' Obtain an instance of RegistryKey for the CurrentUser registry 
        ' root. 
        Dim rkCurrentUser As RegistryKey = Registry.CurrentUser

        ' Obtain the test key (read-only) and display it.
        Dim rkTest As RegistryKey = rkCurrentUser.OpenSubKey("RegistryOpenSubKeyExample")
        Console.WriteLine("Test key: {0}", rkTest)
        rkTest.Close
        rkCurrentUser.Close

        ' Obtain the test key in one step, using the CurrentUser registry 
        ' root.
        rkTest = Registry.CurrentUser.OpenSubKey("RegistryOpenSubKeyExample")
        Console.WriteLine("Test key: {0}", rkTest)
        rkTest.Close

        ' Obtain the test key in read/write mode.
        rkTest = Registry.CurrentUser.OpenSubKey("RegistryOpenSubKeyExample", True)
        rkTest.SetValue("TestName", "TestValue")
        Console.WriteLine("Test value for TestName: {0}", rkTest.GetValue("TestName"))
        rkTest.Close
    End Sub
End Class

Poznámky

Abyste mohli pracovat s jinými metodami a vlastnostmi, musíte klíč otevřít. Chcete-li upravit klíč, musíte jej otevřít s přetížením OpenSubKey metody, která umožňuje zadat přístup k zápisu, například OpenSubKey(String, RegistryKeyPermissionCheck) přetížení nebo OpenSubKey(String, Boolean) přetížení.

Pokud zadaný podklíč nebyl nalezen, null vrátí se.

Chcete-li použít metodu OpenSubKey , musíte mít instanci RegistryKey. Pokud chcete získat instanci RegistryKey, použijte jeden ze statických Registry členů třídy.

Viz také

Platí pro

OpenSubKey(String, RegistryKeyPermissionCheck)

Zdroj:
RegistryKey.cs

Načte zadaný podklíč pro přístup pro čtení nebo čtení a zápis.

public:
 Microsoft::Win32::RegistryKey ^ OpenSubKey(System::String ^ name, Microsoft::Win32::RegistryKeyPermissionCheck permissionCheck);
public Microsoft.Win32.RegistryKey? OpenSubKey(string name, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck);
[System.Runtime.InteropServices.ComVisible(false)]
public Microsoft.Win32.RegistryKey OpenSubKey(string name, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck);
public Microsoft.Win32.RegistryKey OpenSubKey(string name, Microsoft.Win32.RegistryKeyPermissionCheck permissionCheck);
member this.OpenSubKey : string * Microsoft.Win32.RegistryKeyPermissionCheck -> Microsoft.Win32.RegistryKey
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.OpenSubKey : string * Microsoft.Win32.RegistryKeyPermissionCheck -> Microsoft.Win32.RegistryKey
Public Function OpenSubKey (name As String, permissionCheck As RegistryKeyPermissionCheck) As RegistryKey

Parametry

name
String

Název nebo cesta podklíče k vytvoření nebo otevření.

permissionCheck
RegistryKeyPermissionCheck

Jedna z hodnot výčtu, která určuje, zda je klíč otevřen pro přístup pro čtení nebo čtení/zápis.

Návraty

Požadovaný podklíč nebo null pokud operace selhala.

Atributy

Výjimky

name je null

permissionCheck obsahuje neplatnou hodnotu.

Je RegistryKey zavřený (uzavřené klíče nelze získat přístup).

Uživatel nemá oprávnění potřebná ke čtení klíče registru.

Příklady

Následující příklad kódu vytvoří podklíč obsahující 100 párů klíč/hodnota a zavře ho. V příkladu se otevře podklíč s Default, zaznamenává dobu potřebnou ke čtení všech hodnot a zavře podklíč. V příkladu se otevře podklíč a ReadSubTree zaznamená dobu potřebnou ke čtení všech hodnot. Nakonec se příklad vypočítá a zobrazí procentuální zlepšení.

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.
'

Poznámky

Místo vyvolání výjimky tato metoda vrátí null , pokud požadovaný klíč neexistuje.

Pokud permissionCheck je RegistryKeyPermissionCheck.ReadWriteSubTree, klíč je otevřen pro čtení a zápis; pokud permissionCheck je RegistryKeyPermissionCheck.ReadSubTree nebo RegistryKeyPermissionCheck.Default, klíč je otevřen pro čtení, pokud nebyl otevřen nadřazený klíč s RegistryKeyPermissionCheck.ReadWriteSubTree.

Chcete-li použít metodu OpenSubKey , musíte mít instanci RegistryKey třídy. Pokud chcete získat instanci RegistryKey, použijte jeden ze statických Registry členů třídy.

Viz také

Platí pro