Registry クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
Windows レジストリのルート キーを表す RegistryKey オブジェクト、およびキー/値ペアにアクセスするための static
メソッドを提供します。
public ref class Registry abstract sealed
public ref class Registry sealed
public static class Registry
public sealed class Registry
[System.Runtime.InteropServices.ComVisible(true)]
public static class Registry
type Registry = class
[<System.Runtime.InteropServices.ComVisible(true)>]
type Registry = class
Public Class Registry
Public NotInheritable Class Registry
- 継承
-
Registry
- 属性
例
このセクションには、2 つのコード例が含まれています。 最初の例ではルート キーを示し、2 番目の例では メソッドと SetValue メソッドをstatic
GetValue示します。
例 1
次のコード例では、HKEY_USERS キーのサブキーを取得し、その名前を画面に出力する方法を示します。 メソッドを OpenSubKey 使用して、目的の特定のサブキーのインスタンスを作成します。 その後、 で他の RegistryKey
操作を使用して、そのキーを操作できます。
using namespace System;
using namespace Microsoft::Win32;
void PrintKeys( RegistryKey ^ rkey )
{
// Retrieve all the subkeys for the specified key.
array<String^>^names = rkey->GetSubKeyNames();
int icount = 0;
Console::WriteLine( "Subkeys of {0}", rkey->Name );
Console::WriteLine( "-----------------------------------------------" );
// Print the contents of the array to the console.
System::Collections::IEnumerator^ enum0 = names->GetEnumerator();
while ( enum0->MoveNext() )
{
String^ s = safe_cast<String^>(enum0->Current);
Console::WriteLine( s );
// The following code puts a limit on the number
// of keys displayed. Comment it out to print the
// complete list.
icount++;
if ( icount >= 10 )
break;
}
}
int main()
{
// Create a RegistryKey, which will access the HKEY_USERS
// key in the registry of this machine.
RegistryKey ^ rk = Registry::Users;
// Print out the keys.
PrintKeys( rk );
}
using System;
using Microsoft.Win32;
class Reg {
public static void Main() {
// Create a RegistryKey, which will access the HKEY_USERS
// key in the registry of this machine.
RegistryKey rk = Registry.Users;
// Print out the keys.
PrintKeys(rk);
}
static void PrintKeys(RegistryKey rkey) {
// Retrieve all the subkeys for the specified key.
string [] names = rkey.GetSubKeyNames();
int icount = 0;
Console.WriteLine("Subkeys of " + rkey.Name);
Console.WriteLine("-----------------------------------------------");
// Print the contents of the array to the console.
foreach (string s in names) {
Console.WriteLine(s);
// The following code puts a limit on the number
// of keys displayed. Comment it out to print the
// complete list.
icount++;
if (icount >= 10)
break;
}
}
}
Imports Microsoft.Win32
Class Reg
Public Shared Sub Main()
' Create a RegistryKey, which will access the HKEY_USERS
' key in the registry of this machine.
Dim rk As RegistryKey = Registry.Users
' Print out the keys.
PrintKeys(rk)
End Sub
Shared Sub PrintKeys(rkey As RegistryKey)
' Retrieve all the subkeys for the specified key.
Dim names As String() = rkey.GetSubKeyNames()
Dim icount As Integer = 0
Console.WriteLine("Subkeys of " & rkey.Name)
Console.WriteLine("-----------------------------------------------")
' Print the contents of the array to the console.
Dim s As String
For Each s In names
Console.WriteLine(s)
' The following code puts a limit on the number
' of keys displayed. Comment it out to print the
' complete list.
icount += 1
If icount >= 10 Then
Exit For
End If
Next s
End Sub
End Class
例 2
次のコード例では、いくつかのデータ型の値をサンプル キーに格納し、そのキーを作成して、値を取得して表示します。 この例では、既定の (名前のない) 名前と値のペアを格納および取得し、名前と値のペアが存在しない場合の defaultValue
を使用する方法を示します。
using namespace System;
using namespace Microsoft::Win32;
int main()
{
// The name of the key must include a valid root.
String^ userRoot = "HKEY_CURRENT_USER";
String^ subKey = "RegistrySetValueExample2";
String^ keyName = String::Concat(userRoot, "\\", subKey);
// An int value can be stored without specifying the
// registry data type, but Int64 values will be stored
// as strings unless you specify the type. Note that
// the int is stored in the default name/value
// pair.
Registry::SetValue(keyName, "", 5280);
Registry::SetValue(keyName, "TestInt64", 12345678901234,
RegistryValueKind::QWord);
// Strings with expandable environment variables are
// stored as ordinary strings unless you specify the
// data type.
Registry::SetValue(keyName, "TestExpand", "My path: %path%");
Registry::SetValue(keyName, "TestExpand2", "My path: %path%",
RegistryValueKind::ExpandString);
// Arrays of strings are stored automatically as
// MultiString. Similarly, arrays of Byte are stored
// automatically as Binary.
array<String^>^ strings = {"One", "Two", "Three"};
Registry::SetValue(keyName, "TestArray", strings);
// Your default value is returned if the name/value pair
// does not exist.
String^ noSuch = (String^)Registry::GetValue(keyName,
"NoSuchName",
"Return this default if NoSuchName does not exist.");
Console::WriteLine("\r\nNoSuchName: {0}", noSuch);
// Retrieve the int and Int64 values, specifying
// numeric default values in case the name/value pairs
// do not exist. The int value is retrieved from the
// default (nameless) name/value pair for the key.
int testInteger = (int)Registry::GetValue(keyName, "", -1);
Console::WriteLine("(Default): {0}", testInteger);
long long testInt64 = (long long)Registry::GetValue(keyName,
"TestInt64", System::Int64::MinValue);
Console::WriteLine("TestInt64: {0}", testInt64);
// When retrieving a MultiString value, you can specify
// an array for the default return value.
array<String^>^ testArray = (array<String^>^)Registry::GetValue(
keyName, "TestArray",
gcnew array<String^> {"Default if TestArray does not exist."});
for (int i = 0; i < testArray->Length; i++)
{
Console::WriteLine("TestArray({0}): {1}", i, testArray[i]);
}
// A string with embedded environment variables is not
// expanded if it was stored as an ordinary string.
String^ testExpand = (String^)Registry::GetValue(keyName,
"TestExpand", "Default if TestExpand does not exist.");
Console::WriteLine("TestExpand: {0}", testExpand);
// A string stored as ExpandString is expanded.
String^ testExpand2 = (String^)Registry::GetValue(keyName,
"TestExpand2", "Default if TestExpand2 does not exist.");
Console::WriteLine(
"TestExpand2: {0}...", testExpand2->Substring(0, 40));
Console::WriteLine(
"\r\nUse the registry editor to examine the key.");
Console::WriteLine("Press the Enter key to delete the key.");
Console::ReadLine();
Registry::CurrentUser->DeleteSubKey(subKey);
}
//
// This code example produces output similar to the following:
//
// NoSuchName: Return this default if NoSuchName does not exist.
// (Default): 5280
// TestInt64: 12345678901234
// TestArray(0): One
// TestArray(1): Two
// TestArray(2): Three
// TestExpand: My path: %path%
// TestExpand2: My path: D:\Program Files\Microsoft.NET\...
//
// Use the registry editor to examine the key.
// Press the Enter key to delete the key.
using System;
using Microsoft.Win32;
public class Example
{
public static void Main()
{
// The name of the key must include a valid root.
const string userRoot = "HKEY_CURRENT_USER";
const string subkey = "RegistrySetValueExample";
const string keyName = userRoot + "\\" + subkey;
// An int value can be stored without specifying the
// registry data type, but long values will be stored
// as strings unless you specify the type. Note that
// the int is stored in the default name/value
// pair.
Registry.SetValue(keyName, "", 5280);
Registry.SetValue(keyName, "TestLong", 12345678901234,
RegistryValueKind.QWord);
// Strings with expandable environment variables are
// stored as ordinary strings unless you specify the
// data type.
Registry.SetValue(keyName, "TestExpand", "My path: %path%");
Registry.SetValue(keyName, "TestExpand2", "My path: %path%",
RegistryValueKind.ExpandString);
// Arrays of strings are stored automatically as
// MultiString. Similarly, arrays of Byte are stored
// automatically as Binary.
string[] strings = {"One", "Two", "Three"};
Registry.SetValue(keyName, "TestArray", strings);
// Your default value is returned if the name/value pair
// does not exist.
string noSuch = (string) Registry.GetValue(keyName,
"NoSuchName",
"Return this default if NoSuchName does not exist.");
Console.WriteLine("\r\nNoSuchName: {0}", noSuch);
// Retrieve the int and long values, specifying
// numeric default values in case the name/value pairs
// do not exist. The int value is retrieved from the
// default (nameless) name/value pair for the key.
int tInteger = (int) Registry.GetValue(keyName, "", -1);
Console.WriteLine("(Default): {0}", tInteger);
long tLong = (long) Registry.GetValue(keyName, "TestLong",
long.MinValue);
Console.WriteLine("TestLong: {0}", tLong);
// When retrieving a MultiString value, you can specify
// an array for the default return value.
string[] tArray = (string[]) Registry.GetValue(keyName,
"TestArray",
new string[] {"Default if TestArray does not exist."});
for(int i=0; i<tArray.Length; i++)
{
Console.WriteLine("TestArray({0}): {1}", i, tArray[i]);
}
// A string with embedded environment variables is not
// expanded if it was stored as an ordinary string.
string tExpand = (string) Registry.GetValue(keyName,
"TestExpand",
"Default if TestExpand does not exist.");
Console.WriteLine("TestExpand: {0}", tExpand);
// A string stored as ExpandString is expanded.
string tExpand2 = (string) Registry.GetValue(keyName,
"TestExpand2",
"Default if TestExpand2 does not exist.");
Console.WriteLine("TestExpand2: {0}...",
tExpand2.Substring(0, 40));
Console.WriteLine("\r\nUse the registry editor to examine the key.");
Console.WriteLine("Press the Enter key to delete the key.");
Console.ReadLine();
Registry.CurrentUser.DeleteSubKey(subkey);
}
}
//
// This code example produces output similar to the following:
//
//NoSuchName: Return this default if NoSuchName does not exist.
//(Default): 5280
//TestLong: 12345678901234
//TestArray(0): One
//TestArray(1): Two
//TestArray(2): Three
//TestExpand: My path: %path%
//TestExpand2: My path: D:\Program Files\Microsoft.NET\...
//
//Use the registry editor to examine the key.
//Press the Enter key to delete the key.
Imports Microsoft.Win32
Public Class Example
Public Shared Sub Main()
' The name of the key must include a valid root.
Const userRoot As String = "HKEY_CURRENT_USER"
Const subkey As String = "RegistrySetValueExample"
Const keyName As String = userRoot & "\" & subkey
' Integer values can be stored without specifying the
' registry data type, but Long values will be stored
' as strings unless you specify the type. Note that
' the integer is stored in the default name/value
' pair.
Registry.SetValue(keyName, "", 5280)
Registry.SetValue(keyName, "TestLong", 12345678901234, _
RegistryValueKind.QWord)
' Strings with expandable environment variables are
' stored as ordinary strings unless you specify the
' data type.
Registry.SetValue(keyName, "TestExpand", "My path: %path%")
Registry.SetValue(keyName, "TestExpand2", "My path: %path%", _
RegistryValueKind.ExpandString)
' Arrays of strings are stored automatically as
' MultiString. Similarly, arrays of Byte are stored
' automatically as Binary.
Dim strings() As String = {"One", "Two", "Three"}
Registry.SetValue(keyName, "TestArray", strings)
' Your default value is returned if the name/value pair
' does not exist.
Dim noSuch As String = _
Registry.GetValue(keyName, "NoSuchName", _
"Return this default if NoSuchName does not exist.")
Console.WriteLine(vbCrLf & "NoSuchName: {0}", noSuch)
' Retrieve the Integer and Long values, specifying
' numeric default values in case the name/value pairs
' do not exist. The Integer value is retrieved from the
' default (nameless) name/value pair for the key.
Dim tInteger As Integer = _
Registry.GetValue(keyName, "", -1)
Console.WriteLine("(Default): {0}", tInteger)
Dim tLong As Long = Registry.GetValue(keyName, _
"TestLong", Long.MinValue)
Console.WriteLine("TestLong: {0}", tLong)
' When retrieving a MultiString value, you can specify
' an array for the default return value. The value is
' declared inline, but could also be declared as:
' Dim default() As String = {"Default value."}
'
Dim tArray() As String = _
Registry.GetValue(keyName, "TestArray", _
New String() {"Default if TestArray does not exist."})
For i As Integer = 0 To tArray.Length - 1
Console.WriteLine("TestArray({0}): {1}", i, tArray(i))
Next
' A string with embedded environment variables is not
' expanded if it was stored as an ordinary string.
Dim tExpand As String = Registry.GetValue(keyName, _
"TestExpand", "Default if TestExpand does not exist.")
Console.WriteLine("TestExpand: {0}", tExpand)
' A string stored as ExpandString is expanded.
Dim tExpand2 As String = Registry.GetValue(keyName, _
"TestExpand2", "Default if TestExpand2 does not exist.")
Console.WriteLine("TestExpand2: {0}...", _
tExpand2.Substring(0, 40))
Console.WriteLine(vbCrLf & _
"Use the registry editor to examine the key.")
Console.WriteLine("Press the Enter key to delete the key.")
Console.ReadLine()
Registry.CurrentUser.DeleteSubKey(subkey)
End Sub
End Class
'
' This code example produces output similar to the following:
'
'NoSuchName: Return this default if NoSuchName does not exist.
'(Default): 5280
'TestLong: 12345678901234
'TestArray(0): One
'TestArray(1): Two
'TestArray(2): Three
'TestExpand: My path: %path%
'TestExpand2: My path: D:\Program Files\Microsoft.NET\...
'
'Use the registry editor to examine the key.
'Press the Enter key to delete the key.
注釈
このクラスは、Windows を実行しているマシンのレジストリにある標準ルート キーのセットを提供します。 レジストリは、アプリケーション、ユーザー、および既定のシステム設定に関する情報を格納する機能です。 たとえば、アプリケーションはレジストリを使用して、アプリケーションを閉じた後に保持する必要がある情報を格納し、アプリケーションの再読み込み時に同じ情報にアクセスできます。 たとえば、色の設定、画面の場所、またはウィンドウのサイズを格納できます。 レジストリ内の別の場所に情報を格納することで、ユーザーごとにこのデータを制御できます。
クラスによってRegistry
公開される基本インスタンスまたはルート RegistryKey インスタンスは、レジストリ内のサブキーと値の基本的なストレージ メカニズムを示します。 レジストリは存在に依存するため、すべてのキーは読み取り専用です。 によって Registry
公開されるキーは次のとおりです。
CurrentUser ユーザー設定に関する情報を格納します。
LocalMachine ローカル コンピューターの構成情報を格納します。
ClassesRoot 型 (およびクラス) とそのプロパティに関する情報を格納します。
Users 既定のユーザー構成に関する情報を格納します。
PerformanceData ソフトウェア コンポーネントのパフォーマンス情報を格納します。
CurrentConfig ユーザー固有以外のハードウェア情報を格納します。
DynData 動的データを格納します。
レジストリに情報を格納または取得するルート キーを特定したら、 クラスを RegistryKey 使用してサブキーを追加または削除し、特定のキーの値を操作できます。
ハードウェア デバイスは、プラグ アンド プレイ インターフェイスを使用して自動的にレジストリに情報を配置できます。 デバイス ドライバーをインストールするためのソフトウェアでは、標準 API に書き込むことで、レジストリに情報を配置できます。
値を取得および設定するための静的メソッド
Registryクラスには、レジストリ キーの値を設定および取得するための メソッドと SetValue メソッドも含まれていますstatic
GetValue。 これらのメソッドは、レジストリ キーを使用するたびに開いたり閉じるので、多数の値にアクセスすると、 クラスの RegistryKey 類似メソッドと同様に実行されません。
また、 クラスには RegistryKey 、レジストリ キーの Windows アクセス制御セキュリティを設定し、値を取得する前に値のデータ型をテストし、キーを削除するためのメソッドも用意されています。
フィールド
ClassesRoot |
ドキュメントの型 (またはクラス) と、これらの型に関連付けられるプロパティを定義します。 このフィールドには、Windows レジストリの基本キー HKEY_CLASSES_ROOT が読み込まれます。 |
CurrentConfig |
各ユーザーに共通のハードウェアに関する構成情報を格納します。 このフィールドには、Windows レジストリの基本キー HKEY_CURRENT_CONFIG が読み込まれます。 |
CurrentUser |
現在のユーザー設定に関する情報が格納されます。 このフィールドには、Windows レジストリの基本キー HKEY_CURRENT_USER が読み込まれます。 |
DynData |
互換性のために残されています。
互換性のために残されています。
動的レジストリ データを格納します。 このフィールドには、Windows レジストリの基本キー HKEY_DYN_DATA が読み込まれます。 |
LocalMachine |
ローカル コンピューターの構成データが格納されます。 このフィールドには、Windows レジストリの基本キー HKEY_LOCAL_MACHINE が読み込まれます。 |
PerformanceData |
ソフトウェア コンポーネントのパフォーマンス情報が含まれます。 このフィールドには、Windows レジストリの基本キー HKEY_PERFORMANCE_DATA が読み込まれます。 |
Users |
既定のユーザー構成に関する情報を格納します。 このフィールドには、Windows レジストリの基本キー HKEY_USERS が読み込まれます。 |
メソッド
GetValue(String, String, Object) |
指定したレジストリ キーに含まれる、指定した名前に関連付けられた値を取得します。 指定したキーに該当する名前が見つからない場合は、設定している既定値が返されます。指定したキーが存在しない場合は、 |
SetValue(String, String, Object) |
指定したレジストリ キーに、指定した名前/値ペアを設定します。 指定したキーが存在しない場合は、キーが作成されます。 |
SetValue(String, String, Object, RegistryValueKind) |
指定したレジストリ データ型を使用して、指定したレジストリ キーに名前/値ペアを設定します。 指定したキーが存在しない場合は、キーが作成されます。 |