RegistryKey 類別

定義

表示 Windows 登錄中的機碼層級節點。 這個類別的作用是登錄封裝。

public ref class RegistryKey sealed : MarshalByRefObject, IDisposable
public ref class RegistryKey sealed : IDisposable
public sealed class RegistryKey : MarshalByRefObject, IDisposable
public sealed class RegistryKey : IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class RegistryKey : MarshalByRefObject, IDisposable
type RegistryKey = class
    inherit MarshalByRefObject
    interface IDisposable
type RegistryKey = class
    interface IDisposable
[<System.Runtime.InteropServices.ComVisible(true)>]
type RegistryKey = class
    inherit MarshalByRefObject
    interface IDisposable
Public NotInheritable Class RegistryKey
Inherits MarshalByRefObject
Implements IDisposable
Public NotInheritable Class RegistryKey
Implements IDisposable
繼承
繼承
RegistryKey
屬性
實作

範例

下列程式代碼範例示範如何在HKEY_CURRENT_USER下建立子機碼、操作其內容,然後刪除子機碼。

using namespace System;
using namespace System::Security::Permissions;
using namespace Microsoft::Win32;

int main()
{
   // Create a subkey named Test9999 under HKEY_CURRENT_USER.
   RegistryKey ^ test9999 = Registry::CurrentUser->CreateSubKey( "Test9999" );

   // Create two subkeys under HKEY_CURRENT_USER\Test9999.
   test9999->CreateSubKey( "TestName" )->Close();
   RegistryKey ^ testSettings = test9999->CreateSubKey( "TestSettings" );

   // Create data for the TestSettings subkey.
   testSettings->SetValue( "Language", "French" );
   testSettings->SetValue( "Level", "Intermediate" );
   testSettings->SetValue( "ID", 123 );
   testSettings->Close();

   // Print the information from the Test9999 subkey.
   Console::WriteLine( "There are {0} subkeys under Test9999.", test9999->SubKeyCount.ToString() );
   array<String^>^subKeyNames = test9999->GetSubKeyNames();
   for ( int i = 0; i < subKeyNames->Length; i++ )
   {
      RegistryKey ^ tempKey = test9999->OpenSubKey( subKeyNames[ i ] );
      Console::WriteLine( "\nThere are {0} values for {1}.", tempKey->ValueCount.ToString(), tempKey->Name );
      array<String^>^valueNames = tempKey->GetValueNames();
      for ( int j = 0; j < valueNames->Length; j++ )
      {
         Console::WriteLine( "{0,-8}: {1}", valueNames[ j ], tempKey->GetValue( valueNames[ j ] )->ToString() );

      }
   }
   
   // Delete the ID value.
   testSettings = test9999->OpenSubKey( "TestSettings", true );
   testSettings->DeleteValue( "id" );

   // Verify the deletion.
   Console::WriteLine( dynamic_cast<String^>(testSettings->GetValue(  "id", "ID not found." )) );
   testSettings->Close();

   // Delete or close the new subkey.
   Console::Write( "\nDelete newly created registry key? (Y/N) " );
   if ( Char::ToUpper( Convert::ToChar( Console::Read() ) ) == 'Y' )
   {
      Registry::CurrentUser->DeleteSubKeyTree( "Test9999" );
      Console::WriteLine( "\nRegistry key {0} deleted.", test9999->Name );
   }
   else
   {
      Console::WriteLine( "\nRegistry key {0} closed.", test9999->ToString() );
      test9999->Close();
   }
}
using System;
using System.Security.Permissions;
using Microsoft.Win32;

class RegKey
{
    static void Main()
    {
        // Create a subkey named Test9999 under HKEY_CURRENT_USER.
        RegistryKey test9999 =
            Registry.CurrentUser.CreateSubKey("Test9999");
        // Create two subkeys under HKEY_CURRENT_USER\Test9999. The
        // keys are disposed when execution exits the using statement.
        using(RegistryKey
            testName = test9999.CreateSubKey("TestName"),
            testSettings = test9999.CreateSubKey("TestSettings"))
        {
            // Create data for the TestSettings subkey.
            testSettings.SetValue("Language", "French");
            testSettings.SetValue("Level", "Intermediate");
            testSettings.SetValue("ID", 123);
        }

        // Print the information from the Test9999 subkey.
        Console.WriteLine("There are {0} subkeys under {1}.",
            test9999.SubKeyCount.ToString(), test9999.Name);
        foreach(string subKeyName in test9999.GetSubKeyNames())
        {
            using(RegistryKey
                tempKey = test9999.OpenSubKey(subKeyName))
            {
                Console.WriteLine("\nThere are {0} values for {1}.",
                    tempKey.ValueCount.ToString(), tempKey.Name);
                foreach(string valueName in tempKey.GetValueNames())
                {
                    Console.WriteLine("{0,-8}: {1}", valueName,
                        tempKey.GetValue(valueName).ToString());
                }
            }
        }

        using(RegistryKey
            testSettings = test9999.OpenSubKey("TestSettings", true))
        {
            // Delete the ID value.
            testSettings.DeleteValue("id");

            // Verify the deletion.
            Console.WriteLine((string)testSettings.GetValue(
                "id", "ID not found."));
        }

        // Delete or close the new subkey.
        Console.Write("\nDelete newly created registry key? (Y/N) ");
        if(Char.ToUpper(Convert.ToChar(Console.Read())) == 'Y')
        {
            Registry.CurrentUser.DeleteSubKeyTree("Test9999");
            Console.WriteLine("\nRegistry key {0} deleted.",
                test9999.Name);
        }
        else
        {
            Console.WriteLine("\nRegistry key {0} closed.",
                test9999.ToString());
            test9999.Close();
        }
    }
}
Imports System.Security.Permissions
Imports Microsoft.Win32

Public Class RegKey
    Shared Sub Main()

        ' Create a subkey named Test9999 under HKEY_CURRENT_USER.
        Dim test9999 As RegistryKey = _
            Registry.CurrentUser.CreateSubKey("Test9999")

        ' Create two subkeys under HKEY_CURRENT_USER\Test9999.
        test9999.CreateSubKey("TestName").Close()
        Dim testSettings As RegistryKey = _
            test9999.CreateSubKey("TestSettings")

        ' Create data for the TestSettings subkey.
        testSettings.SetValue("Language", "French")
        testSettings.SetValue("Level", "Intermediate")
        testSettings.SetValue("ID", 123)
        testSettings.Close()

        ' Print the information from the Test9999 subkey.
        Console.WriteLine("There are {0} subkeys under Test9999.", _
            test9999.SubKeyCount.ToString())
        For Each subKeyName As String In test9999.GetSubKeyNames()
            Dim tempKey As RegistryKey = _
                test9999.OpenSubKey(subKeyName)
            Console.WriteLine(vbCrLf & "There are {0} values for " & _
                "{1}.", tempKey.ValueCount.ToString(), tempKey.Name)
            For Each valueName As String In tempKey.GetValueNames()
                Console.WriteLine("{0,-8}: {1}", valueName, _
                    tempKey.GetValue(valueName).ToString())
            Next
        Next

        ' Delete the ID value.
        testSettings = test9999.OpenSubKey("TestSettings", True)
        testSettings.DeleteValue("id")

        ' Verify the deletion.
        Console.WriteLine(CType(testSettings.GetValue( _
            "id", "ID not found."), String))
        testSettings.Close()

        ' Delete or close the new subkey.
        Console.Write(vbCrLf & "Delete newly created " & _
            "registry key? (Y/N) ")
        If Char.ToUpper(Convert.ToChar(Console.Read())) = "Y"C Then
            Registry.CurrentUser.DeleteSubKeyTree("Test9999")
            Console.WriteLine(vbCrLf & "Registry key {0} deleted.", _
                test9999.Name)
        Else
            Console.WriteLine(vbCrLf & "Registry key {0} closed.", _
                test9999.ToString())
            test9999.Close()
        End If
   
    End Sub
End Class

備註

若要取得的 RegistryKey實例,請使用 類別的 Registry 其中一個靜態成員。

登錄可作為計算機上作業系統和應用程式資訊的中央存放庫。 登錄是以階層式格式組織,根據儲存在其內的元素邏輯順序, (請參閱 Registry 此階層中的基底層級專案) 。 在登錄中儲存資訊時,請根據所儲存的資訊類型選取適當的位置。 請務必避免終結其他應用程式所建立的信息,因為這可能會導致這些應用程式顯示非預期的行為,而且也會對您自己的應用程式造成負面影響。

重要

此型別代表 IDisposable 介面。 當您完成使用型別時,您應該直接或間接處置它。 若要直接處置型別,請呼叫其 try/catch 區塊中的 Dispose 方法。 若要間接處置它,請使用語言建構函式,例如 using (在 C# 中) 或 Using (在 Visual Basic 中)。 如需詳細資訊,請參閱 IDisposable 介面文章中的<使用實作 IDisposable 的物件>一節。

登錄機碼是登錄中組織的基底單位,而且可以與 檔案總管 中的資料夾進行比較。 特定索引鍵可以有子機碼,就像資料夾可以有子資料夾一樣。 只要使用者有適當的許可權可以刪除每個密鑰,而且金鑰不是基底密鑰,或直接在基底索引鍵底下層級即可。 每個索引鍵也可以有多個與其相關聯的值, (值可以與用來儲存資訊的檔案) 進行比較,例如,計算機上所安裝應用程式的相關信息。 每個值都會保留一個特定的資訊片段,必要時可以擷取或更新。 例如,您可以在公司的密鑰 HKEY_LOCAL_MACHINE\Software 下,為公司建立 RegistryKey 子機碼,然後為公司建立的每個應用程式建立子機碼。 每個子機碼都會保留該應用程式特有的資訊,例如色彩設定、螢幕位置和大小,或辨識的擴展名。

請注意,儲存在登錄中的資訊可供其他應用程式和使用者使用,因此不應該用來儲存安全性數據或重要應用程序資訊。

警告

請勿以惡意程式可能會建立數千個無意義的子機碼或索引鍵/值組的方式來公開 RegistryKey 物件。 例如,不允許呼叫端輸入任意索引鍵或值。

從 .NET Framework 4 開始,登錄機碼的長度不再限製為 255 個字元。

屬性

Handle

取得 SafeRegistryHandle 物件,這個物件表示目前 RegistryKey 物件所封裝的登錄機碼。

Name

擷取機碼名稱。

SubKeyCount

擷取目前機碼中子機碼的計數。

ValueCount

擷取機碼中值的計數。

View

取得用來建立登錄機碼的檢視。

方法

Close()

如果其內容已經做了修改,請關閉機碼,並將它清除至磁碟中。

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
CreateSubKey(String)

為寫入權限建立新的子機碼,或開啟現有的子機碼。

CreateSubKey(String, Boolean)

以指定權限建立新的子機碼,或開啟現有的子機碼。 從 .NET Framework 4.6 開始可供使用。

CreateSubKey(String, Boolean, RegistryOptions)

以指定權限建立新的子機碼,或開啟現有的子機碼。 從 .NET Framework 4.6 開始可供使用。

CreateSubKey(String, RegistryKeyPermissionCheck)

使用指定的權限檢查選項,為寫入權限建立新的子機碼,或開啟現有的子機碼。

CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions)

使用指定的權限檢查和登錄選項,為寫入權限建立子機碼或開啟子機碼。

CreateSubKey(String, RegistryKeyPermissionCheck, RegistryOptions, RegistrySecurity)

使用指定的權限檢查選項、登錄選項和登錄安全性,為寫入權限建立子機碼或開啟子機碼。

CreateSubKey(String, RegistryKeyPermissionCheck, RegistrySecurity)

使用指定的權限檢查選項和登錄安全性,為寫入權限建立新的子機碼,或開啟現有的子機碼。

DeleteSubKey(String)

刪除指定的子機碼。

DeleteSubKey(String, Boolean)

刪除指定的子機碼,並且指定在找不到該子機碼時是否引發例外狀況。

DeleteSubKeyTree(String)

遞迴地刪除子機碼和任何子系子機碼。

DeleteSubKeyTree(String, Boolean)

遞迴地刪除指定的子機碼和任何子系子機碼,並且指定在找不到該子機碼時是否引發例外狀況。

DeleteValue(String)

從這個機碼中刪除指定值。

DeleteValue(String, Boolean)

刪除此機碼的指定值,並且指定找不到該值時是否引發例外狀況。

Dispose()

釋放 RegistryKey 類別目前的執行個體所使用的全部資源。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Finalize()

如果內容已修改,請關閉機碼並進行磁碟排清。

Flush()

將指定的開啟登錄機碼的所有屬性 (Attribute) 寫入登錄中。

FromHandle(SafeRegistryHandle)

從指定的控制代碼建立登錄機碼。

FromHandle(SafeRegistryHandle, RegistryView)

從指定的控制代碼和登錄檢視設定,建立登錄機碼。

GetAccessControl()

傳回目前登錄機碼的存取控制安全性。

GetAccessControl(AccessControlSections)

傳回目前登錄機碼之存取控制安全性的指定區段。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetSubKeyNames()

擷取包含所有子機碼名稱的字串陣列。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
GetValue(String)

擷取與指定名稱關聯的值。 如果登錄中沒有名稱/值組,則傳回 null

GetValue(String, Object)

擷取與指定名稱關聯的值。 如果找不到名稱,則傳回您提供的預設值。

GetValue(String, Object, RegistryValueOptions)

擷取與指定名稱及擷取選項關聯的值。 如果找不到名稱,則傳回您提供的預設值。

GetValueKind(String)

擷取與指定名稱關聯之值的登錄資料類型。

GetValueNames()

擷取包含所有與這個機碼相關值名稱的字串陣列。

InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
OpenBaseKey(RegistryHive, RegistryView)

使用指定的檢視,開啟本機電腦上表示要求機碼的新 RegistryKey

OpenRemoteBaseKey(RegistryHive, String)

開啟遠端電腦上表示要求機碼的新 RegistryKey

OpenRemoteBaseKey(RegistryHive, String, RegistryView)

使用指定的檢視,開啟遠端電腦上表示要求機碼的新登錄機碼。

OpenSubKey(String)

擷取子機碼為唯讀。

OpenSubKey(String, Boolean)

擷取指定的子機碼,並且指定此機碼是否要套用寫入存取權限。

OpenSubKey(String, RegistryKeyPermissionCheck)

擷取指定的子機碼,以供讀取或讀取/寫入存取。

OpenSubKey(String, RegistryKeyPermissionCheck, RegistryRights)

擷取指定的子機碼以供讀取或讀取/寫入存取,並要求指定的存取權限。

OpenSubKey(String, RegistryRights)

擷取具有指定名稱和存取權限的子機碼。 從 .NET Framework 4.6 開始可供使用。

SetAccessControl(RegistrySecurity)

將 Windows 存取控制安全性套用至現有的登錄機碼。

SetValue(String, Object)

設定指定的名稱/值組。

SetValue(String, Object, RegistryValueKind)

使用指定的登錄資料類型,設定登錄機碼中名稱/值組的值。

ToString()

擷取這個機碼的字串表示。

明確介面實作

IDisposable.Dispose()

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

對目前的機碼執行 Close()

擴充方法

GetAccessControl(RegistryKey)

傳回登錄機碼的安全性資訊。

GetAccessControl(RegistryKey, AccessControlSections)

傳回登錄機碼的安全性資訊。

SetAccessControl(RegistryKey, RegistrySecurity)

變更現有登錄機碼的安全性屬性。

適用於

另請參閱