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)
擷取指定的子機碼,並且指定此機碼是否要套用寫入存取權限。
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)
擷取指定的子機碼以供讀取或讀取/寫入存取,並要求指定的存取權限。
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.ReadSubTreepermissionCheck
指定 和 RegistryRights.WriteKeyrights
,則嘗試寫入子機碼會擲回例外狀況。
若要使用 OpenSubKey 方法,您必須擁有 類別的 RegistryKey 實例。 若要取得的 RegistryKey實例,請使用 類別 Registry 的其中一個靜態成員。
另請參閱
- CreateSubKey(String)
- DeleteSubKey(String)
- DeleteSubKeyTree(String)
- GetSubKeyNames()
- OpenRemoteBaseKey(RegistryHive, String)
- SubKeyCount
- Registry
適用於
OpenSubKey(String, RegistryRights)
擷取具有指定名稱和存取權限的子機碼。 從 .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)
擷取子機碼為唯讀。
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)
擷取指定的子機碼,以供讀取或讀取/寫入存取。
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