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
- 屬性
範例
本節包含兩個程式碼範例。 第一個範例示範根索引鍵,第二個範例示範 static
GetValue 和 SetValue 方法。
範例 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 也包含 static
GetValue 和 SetValue 方法,可從登錄機碼設定和擷取值。 這些方法會在每次使用時開啟和關閉登錄機碼,因此當您存取大量值時,它們不會在 類別中 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) |
使用指定的登錄資料型別,在指定的登錄機碼上設定名稱/值組。 如果指定的機碼不存在,則會建立它。 |
適用於
另請參閱
意見反應
提交並檢視相關的意見反應