RegistryKey.OpenSubKey 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索指定的子项。
重载
OpenSubKey(String, Boolean) |
检索指定的子项,并指定是否将写访问权限应用于该项。 |
OpenSubKey(String, RegistryKeyPermissionCheck, RegistryRights) |
检索指定的子项以进行读取或读/写访问,请求指定的访问权限。 |
OpenSubKey(String, RegistryRights) |
检索具有指定名称和访问权限的子项。 从 .NET Framework 4.6 开始可用。 |
OpenSubKey(String) |
以只读方式检索子项。 |
OpenSubKey(String, RegistryKeyPermissionCheck) |
检索指定的子项以进行读取或读/写访问。 |
OpenSubKey(String, Boolean)
- Source:
- RegistryKey.cs
检索指定的子项,并指定是否将写访问权限应用于该项。
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
参数
- name
- String
要打开的子项的名称或路径。
- writable
- Boolean
如果需要项的写访问权限,则设置为 true
。
返回
请求的子项;如果操作失败,则为 null
。
例外
name
为 null
。
RegistryKey 已关闭(无法访问已关闭的项)。
用户没有在指定模式下访问注册表项所需的权限。
示例
下面的代码示例创建一个测试密钥,并使用 OpenSubKey 方法打开它,并演示方法的两个重载。
#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
注解
如果请求的键不存在,此方法将返回 null
,而不是引发异常。
如果 writable
为 true
,则将打开密钥进行读取和写入,否则,密钥将作为只读打开。
若要使用 OpenSubKey 方法,必须具有 方法的 RegistryKey 实例。 若要获取 的 RegistryKey实例,请使用 类的静态成员之 Registry 一。
另请参阅
- CreateSubKey(String)
- DeleteSubKey(String)
- DeleteSubKeyTree(String)
- GetSubKeyNames()
- OpenRemoteBaseKey(RegistryHive, String)
- SubKeyCount
- Registry
适用于
OpenSubKey(String, RegistryKeyPermissionCheck, RegistryRights)
- Source:
- RegistryKey.cs
检索指定的子项以进行读取或读/写访问,请求指定的访问权限。
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);
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);
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
参数
- name
- String
要创建或打开的子项的名称或路径。
- permissionCheck
- RegistryKeyPermissionCheck
用于指定打开该项是进行读取还是读取/写入访问的枚举值之一。
- rights
- RegistryRights
枚举值的按位组合,它指定所需的安全访问。
返回
请求的子项;如果操作失败,则为 null
。
- 属性
例外
name
为 null
permissionCheck
包含无效值。
RegistryKey 已关闭(无法访问已关闭的项)。
注解
如果请求的键不存在,此方法将返回 null
,而不是引发异常。
如果 permissionCheck
为 RegistryKeyPermissionCheck.ReadWriteSubTree,则打开密钥进行读取和写入;如果 permissionCheck
为 RegistryKeyPermissionCheck.ReadSubTree 或 RegistryKeyPermissionCheck.Default,则打开密钥以供读取,除非使用 RegistryKeyPermissionCheck.ReadWriteSubTree打开父键。
为 permissionCheck
指定的访问权限优先于为 rights
指定的访问权限。 例如,如果为 指定 RegistryKeyPermissionCheck.ReadSubTree 和 permissionCheck
RegistryRights.WriteKey , rights
则尝试写入子项会引发异常。
若要使用 OpenSubKey 方法,必须具有 类的 RegistryKey 实例。 若要获取 的 RegistryKey实例,请使用 类的静态成员之 Registry 一。
另请参阅
- CreateSubKey(String)
- DeleteSubKey(String)
- DeleteSubKeyTree(String)
- GetSubKeyNames()
- OpenRemoteBaseKey(RegistryHive, String)
- SubKeyCount
- Registry
适用于
OpenSubKey(String, RegistryRights)
- Source:
- RegistryKey.cs
检索具有指定名称和访问权限的子项。 从 .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
参数
- name
- String
要创建或打开的子项的名称或路径。
- rights
- RegistryRights
注册表项的权限。
返回
请求的子项;如果操作失败,则为 null
。
- 属性
例外
name
为 null
。
RegistryKey 已关闭(无法访问已关闭的项)。
用户没有在指定模式下访问注册表项所需的权限。
注解
必须先打开密钥,然后才能使用其他方法和属性操作密钥。 若要修改密钥,必须使用 方法的 OpenSubKey 重载打开它,以便指定写入访问权限。
适用于
OpenSubKey(String)
- Source:
- RegistryKey.cs
以只读方式检索子项。
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
参数
- name
- String
要以只读方式打开的子项的名称或路径。
返回
请求的子项;如果操作失败,则为 null
。
例外
name
为 null
RegistryKey 已关闭(无法访问已关闭的项)。
用户没有读取注册表项所需的权限。
示例
下面的代码示例创建一个测试密钥,并使用 OpenSubKey 方法打开它,并演示方法的两个重载。
#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
注解
必须先打开密钥,然后才能使用其他方法和属性操作密钥。 若要修改键,必须使用 方法的 OpenSubKey 重载打开它,该方法允许指定写入访问权限,例如 OpenSubKey(String, RegistryKeyPermissionCheck) 重载或 OpenSubKey(String, Boolean) 重载。
如果找不到指定的子项,则 null
返回 。
若要使用 OpenSubKey 方法,必须具有 的 RegistryKey实例。 若要获取 的 RegistryKey实例,请使用 类的静态成员之 Registry 一。
另请参阅
- CreateSubKey(String)
- DeleteSubKey(String)
- DeleteSubKeyTree(String)
- GetSubKeyNames()
- OpenRemoteBaseKey(RegistryHive, String)
- SubKeyCount
- Registry
适用于
OpenSubKey(String, RegistryKeyPermissionCheck)
- Source:
- RegistryKey.cs
检索指定的子项以进行读取或读/写访问。
public:
Microsoft::Win32::RegistryKey ^ OpenSubKey(System::String ^ name, Microsoft::Win32::RegistryKeyPermissionCheck permissionCheck);
public Microsoft.Win32.RegistryKey OpenSubKey (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);
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
参数
- name
- String
要创建或打开的子项的名称或路径。
- permissionCheck
- RegistryKeyPermissionCheck
用于指定打开该项是进行读取还是读取/写入访问的枚举值之一。
返回
请求的子项;如果操作失败,则为 null
。
- 属性
例外
name
为 null
permissionCheck
包含无效值。
RegistryKey 已关闭(无法访问已关闭的项)。
用户没有读取注册表项所需的权限。
示例
下面的代码示例创建一个包含 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.
'
注解
如果请求的键不存在,此方法将返回 null
,而不是引发异常。
如果 permissionCheck
为 RegistryKeyPermissionCheck.ReadWriteSubTree,则打开密钥进行读取和写入;如果 permissionCheck
为 RegistryKeyPermissionCheck.ReadSubTree 或 RegistryKeyPermissionCheck.Default,则打开密钥以供读取,除非使用 RegistryKeyPermissionCheck.ReadWriteSubTree打开父键。
若要使用 OpenSubKey 方法,必须具有 类的 RegistryKey 实例。 若要获取 的 RegistryKey实例,请使用 类的静态成员之 Registry 一。
另请参阅
- CreateSubKey(String)
- DeleteSubKey(String)
- DeleteSubKeyTree(String)
- GetSubKeyNames()
- OpenRemoteBaseKey(RegistryHive, String)
- SubKeyCount
- Registry