RegistryKey クラス
アセンブリ: mscorlib (mscorlib.dll 内)

<ComVisibleAttribute(True)> _ Public NotInheritable Class RegistryKey Inherits MarshalByRefObject Implements IDisposable
[ComVisibleAttribute(true)] public ref class RegistryKey sealed : public MarshalByRefObject, IDisposable

RegistryKey のインスタンスを取得するには、Registry クラスの静的メンバの 1 つを使用します。
レジストリは、オペレーティング システムとコンピュータ上のアプリケーションの情報を格納した中央リポジトリとして機能します。レジストリは、レジストリに格納されている要素の論理的な順序に基づいて、階層形式で編成されています。ベース レベルの項目については、Registry のトピックを参照してください。情報をレジストリに格納するときは、この情報のタイプに基づいて適切な位置を選択します。他のアプリケーションにより作成された情報を壊さないように注意してください。情報が壊れると、予測できない動作をアプリケーションが行ったり、作成したアプリケーションが悪影響を受けたりする可能性があります。
レジストリ キーは、レジストリの基本編成単位です。Windows エクスプローラのフォルダに該当します。キーはサブキーを持つことができますが、これはフォルダがサブフォルダを持つことができるのと同様です。各キーは削除できますが、削除するためには、適切なアクセス許可がユーザーに設定されている必要があります。なお、基本キーおよび基本キーの直下に置かれているキーは削除できません。また、各キーには、複数の値を関連付けることができます。値はファイルに該当するものであり、情報 (たとえば、コンピュータにインストールされているアプリケーションに関する情報) を格納するために使用します。それぞれの値は特定の情報を格納しており、必要に応じてこの情報を取得したり更新したりできます。たとえば、HKEY_LOCAL_MACHINE\Software キーの下に会社の RegistryKey を作成し、会社で作成する各アプリケーションを表すサブキーを作成できます。各サブキーは、カラー設定、画面の位置とサイズ、認識されるファイル拡張子など、各アプリケーション固有の情報を保持します。
レジストリに格納されている情報は、他のアプリケーションやユーザーが使用できるため、レジストリにはセキュリティ関連データや重要なアプリケーション情報を格納しないでください。
![]() |
---|
悪意のあるプログラムによって無効なサブキーと値、またはキーと値のペアが無数に作成されるような方法では、RegistryKey オブジェクトを公開しないでください。たとえば、呼び出し元が任意のキーまたは値を入力できるようにしないでください。 |
Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows CE プラットフォームメモ : .NET Compact Framework アプリケーションでは、サブキーを削除する前に、このサブキーおよびその子サブキーの開いているすべてのインスタンスを明示的に閉じる必要があります。Windows CE によって決定されるサブキーの深さの最大値は 15 です。

HKEY_CURRENT_USER の下にサブキーを作成し、その内容を操作し、その後にサブキーを削除する方法を次のコード例に示します。
Imports Microsoft.VisualBasic Imports System Imports System.Security.Permissions Imports Microsoft.Win32 <Assembly: RegistryPermissionAttribute( _ SecurityAction.RequestMinimum, ViewAndModify := "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.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
using System; using System.Security.Permissions; using Microsoft.Win32; [assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, ViewAndModify = "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); } // 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(); } } }
using namespace System; using namespace System::Security::Permissions; using namespace Microsoft::Win32; [assembly:RegistryPermissionAttribute(SecurityAction::RequestMinimum, ViewAndModify="HKEY_CURRENT_USER")]; 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(); } }

System.MarshalByRefObject
Microsoft.Win32.RegistryKey


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


RegistryKey プロパティ
RegistryKey メソッド



RegistryKey メンバ
Windows レジストリのキー レベル ノードを表します。このクラスはレジストリをカプセル化します。
RegistryKey データ型で公開されるメンバを以下の表に示します。




- RegistryKeyのページへのリンク