Share via


RegistryKey クラス

Windows レジストリのキー レベル ノードを表します。このクラスはレジストリをカプセル化します。

この型のすべてのメンバの一覧については、RegistryKey メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      Microsoft.Win32.RegistryKey

NotInheritable Public Class RegistryKey
   Inherits MarshalByRefObject
   Implements IDisposable
[C#]
public sealed class RegistryKey : MarshalByRefObject, IDisposable
[C++]
public __gc __sealed class RegistryKey : public MarshalByRefObject,
   IDisposable
[JScript]
public class RegistryKey extends MarshalByRefObject implements
   IDisposable

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

レジストリは、オペレーティング システムとコンピュータ上のアプリケーションの情報を格納した中央リポジトリとして機能します。レジストリは、レジストリに格納されている要素の論理的な順序に基づいて、階層形式で編成されています。ベース レベルの項目については、 Registry のトピックを参照してください。情報をレジストリに格納すると、この情報のタイプに基づいて適切な位置が選択されます。他のアプリケーションにより作成された情報を壊さないように注意してください。情報が壊れると、予測できない動作をアプリケーションが行ったり、作成したアプリケーションが悪影響を受けたりする可能性があります。

RegistryKeys は、レジストリの基本編成単位です。Windows エクスプローラのフォルダに該当します。フォルダの中にサブフォルダを作成できるのと同様に、特定のキーの下にサブキーを作成できます。キーを削除できる許可が設定されている場合は、基本キーと基本キーの直下のレベルのキー以外のキーを削除できます。各キーに対して複数の値を関連付けることができます。この値は、エクスプローラのファイルに該当します。特定のアプリケーションの情報を格納するためにこれらの値が使用されます。それぞれの値は特定の情報を維持します。必要に応じてこの情報を取得または更新できます。たとえば、HKEY_LOCAL_MACHINE\Software キーの下に会社の RegistryKey を作成し、会社で作成する各アプリケーションを表すサブキーを作成できます。各サブキーは、カラー設定、画面の位置とサイズ、認識されるファイル拡張子など、各アプリケーション固有の情報を保持します。

レジストリに格納されている情報は、他のアプリケーションやユーザーに対しても使用できるため、レジストリにはセキュリティ関連情報や重要なアプリケーション情報を格納しないでください。

RegistryKey のインスタンスを取得するには、 OpenSubKey 静的メンバを使用するか、または Registry クラスの静的メンバを使用します。

使用例

[Visual Basic, C#, C++] HKEY_CURRENT_USER の下にサブキーを作成し、その内容を操作し、その後にサブキーを削除する方法を次のコード例に示します。

 
Imports Microsoft.VisualBasic
Imports System
Imports System.Security.Permissions
Imports Microsoft.Win32

<Assembly: RegistryPermissionAttribute( _
    SecurityAction.RequestMinimum, All := "HKEY_CURRENT_USER")>

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.SetValue("Password", "Secret")
        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 non-secure password value.
        testSettings = test9999.OpenSubKey("TestSettings", True)
        testSettings.DeleteValue("password")

        ' Verify the deletion.
        Console.WriteLine(CType(testSettings.GetValue( _
            "password", "Password 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

[C#] 
using System;
using System.Security.Permissions;
using Microsoft.Win32;

[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum,
    All = "HKEY_CURRENT_USER")]

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);
            testSettings.SetValue("Password", "Secret");
        }

        // 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 non-secure password value.
            testSettings.DeleteValue("password");

            // Verify the deletion.
            Console.WriteLine((string)testSettings.GetValue(
                "password", "Password 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();
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Security::Permissions;
using namespace Microsoft::Win32;

[assembly: RegistryPermissionAttribute(SecurityAction::RequestMinimum,
    All = S"HKEY_CURRENT_USER")];

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

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

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

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

    // Delete the non-secure password value.
    testSettings = test9999->OpenSubKey(S"TestSettings", true);
    testSettings->DeleteValue(S"password");

    // Verify the deletion.
    Console::WriteLine(dynamic_cast<String*>(
        testSettings->GetValue("password", S"Password not found.")));
    testSettings->Close();

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

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: Microsoft.Win32

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

RegistryKey メンバ | Microsoft.Win32 名前空間 | Registry | RegistryHive