RegistryKey.GetValue 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
擷取與指定名稱關聯的值。
多載
GetValue(String, Object, RegistryValueOptions) |
擷取與指定名稱及擷取選項關聯的值。 如果找不到名稱,則傳回您提供的預設值。 |
GetValue(String) |
擷取與指定名稱關聯的值。 如果登錄中沒有名稱/值組,則傳回 |
GetValue(String, Object) |
擷取與指定名稱關聯的值。 如果找不到名稱,則傳回您提供的預設值。 |
GetValue(String, Object, RegistryValueOptions)
擷取與指定名稱及擷取選項關聯的值。 如果找不到名稱,則傳回您提供的預設值。
public:
System::Object ^ GetValue(System::String ^ name, System::Object ^ defaultValue, Microsoft::Win32::RegistryValueOptions options);
public object GetValue (string name, object defaultValue, Microsoft.Win32.RegistryValueOptions options);
public object? GetValue (string? name, object? defaultValue, Microsoft.Win32.RegistryValueOptions options);
[System.Runtime.InteropServices.ComVisible(false)]
public object GetValue (string name, object defaultValue, Microsoft.Win32.RegistryValueOptions options);
member this.GetValue : string * obj * Microsoft.Win32.RegistryValueOptions -> obj
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.GetValue : string * obj * Microsoft.Win32.RegistryValueOptions -> obj
Public Function GetValue (name As String, defaultValue As Object, options As RegistryValueOptions) As Object
參數
- name
- String
要擷取的值的名稱。 這個字串不會區分大小寫。
- defaultValue
- Object
name
不存在時所傳回的值。
- options
- RegistryValueOptions
其中一個列舉值,指定擷取之值的選擇性處理。
傳回
與 name
關聯的值,根據指定的 options
處理,如果找不到 defaultValue
,則為 name
。
- 屬性
例外狀況
使用者沒有讀取登錄機碼所需的使用權限。
包含指定值的 RegistryKey 已經關閉 (關閉的機碼無法存取)。
包含指定值的 RegistryKey 已標記為刪除。
options
不是有效的 RegistryValueOptions 值,例如將無效的值轉換成 RegistryValueOptions。
使用者沒有必要的登錄權限。
範例
下列程式代碼範例會建立測試索引鍵、新增具有內嵌環境變數的值,並在展開和未展開的窗體中擷取值。
#using <Microsoft.VisualBasic.dll>
using namespace System;
using namespace Microsoft::Win32;
using namespace Microsoft::VisualBasic;
int main()
{
// Delete and recreate the test key.
Registry::CurrentUser->DeleteSubKey( L"RegistryValueOptionsExample", false );
RegistryKey ^ rk =
Registry::CurrentUser->CreateSubKey( L"RegistryValueOptionsExample" );
// Add a value that contains an environment variable.
rk->SetValue( L"ExpandValue", L"The path is %PATH%",
RegistryValueKind::ExpandString );
// Retrieve the value, first without expanding the environment
// variable and then expanding it.
Console::WriteLine( L"Unexpanded: \"{0}\"",
rk->GetValue( L"ExpandValue",
L"No Value",
RegistryValueOptions::DoNotExpandEnvironmentNames ) );
Console::WriteLine( L"Expanded: \"{0}\"", rk->GetValue( L"ExpandValue" ) );
return 0;
} //Main
using System;
using Microsoft.Win32;
using Microsoft.VisualBasic;
public class Example
{
public static void Main()
{
// Delete and recreate the test key.
Registry.CurrentUser.DeleteSubKey("RegistryValueOptionsExample", false);
RegistryKey rk =
Registry.CurrentUser.CreateSubKey("RegistryValueOptionsExample");
// Add a value that contains an environment variable.
rk.SetValue("ExpandValue", "The path is %PATH%", RegistryValueKind.ExpandString);
// Retrieve the value, first without expanding the environment
// variable and then expanding it.
Console.WriteLine("Unexpanded: \"{0}\"",
rk.GetValue("ExpandValue", "No Value",
RegistryValueOptions.DoNotExpandEnvironmentNames));
Console.WriteLine("Expanded: \"{0}\"", rk.GetValue("ExpandValue"));
} //Main
} //Example
Imports Microsoft.Win32
Public Class Example
Public Shared Sub Main()
' Delete and recreate the test key.
Registry.CurrentUser.DeleteSubKey("RegistryValueOptionsExample", False)
Dim rk As RegistryKey = _
Registry.CurrentUser.CreateSubKey("RegistryValueOptionsExample")
' Add a value that contains an environment variable.
rk.SetValue("ExpandValue", "The path is %PATH%", _
RegistryValueKind.ExpandString)
' Retrieve the value, first without expanding the environment
' variable and then expanding it.
Console.WriteLine("Unexpanded: ""{0}""", _
rk.GetValue("ExpandValue", "No Value", _
RegistryValueOptions.DoNotExpandEnvironmentNames))
Console.WriteLine("Expanded: ""{0}""", rk.GetValue("ExpandValue"))
End Sub
End Class
備註
使用此多載來指定擷取值的特殊處理。 例如,您可以在擷取類型的RegistryValueKind.ExpandString登錄值時指定 RegistryValueOptions.DoNotExpandEnvironmentNames ,以擷取字串而不展開內嵌環境變數。
defaultValue
使用 參數可指定要傳回的值,如果name
不存在則傳回。
注意
登錄機碼可以有一個未與任何名稱相關聯的值。 當登錄編輯器中顯示這個未命名的值時,會出現 「 (Default) 」 字串,而不是名稱。 若要擷取這個未命名的值,請為 指定 null
空字串 (“”“) name
。
GetValue 不支援讀取類型為 REG_NONE 或 REG_LINK的值。 在這兩種情況下,都會傳回預設值 (null
) ,而不是實際值。
另請參閱
適用於
GetValue(String)
擷取與指定名稱關聯的值。 如果登錄中沒有名稱/值組,則傳回 null
。
public:
System::Object ^ GetValue(System::String ^ name);
public object GetValue (string name);
public object? GetValue (string? name);
member this.GetValue : string -> obj
Public Function GetValue (name As String) As Object
參數
- name
- String
要擷取的值的名稱。 這個字串不會區分大小寫。
傳回
與 name
關聯的值,如果找不到 name
,則為 null
。
例外狀況
使用者沒有讀取登錄機碼所需的使用權限。
包含指定值的 RegistryKey 已經關閉 (關閉的機碼無法存取)。
包含指定值的 RegistryKey 已標記為刪除。
使用者沒有必要的登錄權限。
範例
下列程式代碼範例會建立測試索引鍵,並將不同數據類型的值新增至索引鍵。 然後,此範例會讀取名稱/值組,並將其顯示給控制台,方法是使用 GetValueKind 方法來擷取對應的登錄數據類型。
using namespace System;
using namespace Microsoft::Win32;
int main()
{
// Delete and recreate the test key.
Registry::CurrentUser->DeleteSubKey( "RegistryValueKindExample", false );
RegistryKey ^ rk = Registry::CurrentUser->CreateSubKey( "RegistryValueKindExample" );
// Create name/value pairs.
// This overload supports QWord (long) values.
rk->SetValue( "QuadWordValue", 42, RegistryValueKind::QWord );
// The following SetValue calls have the same effect as using the
// SetValue overload that does not specify RegistryValueKind.
//
rk->SetValue( "DWordValue", 42, RegistryValueKind::DWord );
rk->SetValue( "MultipleStringValue", gcnew array<String^>{
"One","Two","Three"
}, RegistryValueKind::MultiString );
rk->SetValue( "BinaryValue", gcnew array<Byte>{
10,43,44,45,14,255
}, RegistryValueKind::Binary );
rk->SetValue( "StringValue", "The path is %PATH%", RegistryValueKind::String );
// This overload supports setting expandable string values. Compare
// the output from this value with the previous string value.
rk->SetValue( "ExpandedStringValue", "The path is %PATH%", RegistryValueKind::ExpandString );
// Display all the name/value pairs stored in the test key, with the
// registry data type in parentheses.
//
array<String^>^valueNames = rk->GetValueNames();
System::Collections::IEnumerator^ myEnum = valueNames->GetEnumerator();
while ( myEnum->MoveNext() )
{
String^ s = safe_cast<String^>(myEnum->Current);
RegistryValueKind rvk = rk->GetValueKind( s );
switch ( rvk )
{
case RegistryValueKind::MultiString:
{
array<String^>^values = (array<String^>^)rk->GetValue( s );
Console::Write( "\r\n {0} ({1}) =", s, rvk );
for ( int i = 0; i < values->Length; i++ )
{
if (i != 0) Console::Write(",");
Console::Write( " \"{0}\"", values[ i ] );
}
Console::WriteLine();
break;
}
case RegistryValueKind::Binary:
{
array<Byte>^bytes = (array<Byte>^)rk->GetValue( s );
Console::Write( "\r\n {0} ({1}) =", s, rvk );
for ( int i = 0; i < bytes->Length; i++ )
{
// Display each byte as two hexadecimal digits.
Console::Write( " {0:X2}", bytes[ i ] );
}
Console::WriteLine();
break;
}
default:
Console::WriteLine( "\r\n {0} ({1}) = {2}", s, rvk, rk->GetValue( s ) );
break;
}
}
}
/*
This code example produces the following output:
QuadWordValue (QWord) = 42
DWordValue (DWord) = 42
MultipleStringValue (MultiString) =, "One", "Two", "Three"
BinaryValue (Binary) = 0A 2B 2C 2D 0E FF
StringValue (String) = The path is %PATH%
ExpandedStringValue (ExpandString) = The path is C:\Program Files\Microsoft.NET\SDK\v2.0\Bin;
[***The remainder of this output is omitted.***]
*/
using System;
using Microsoft.Win32;
public class Example
{
public static void Main()
{
// Delete and recreate the test key.
Registry.CurrentUser.DeleteSubKey("RegistryValueKindExample", false);
RegistryKey rk = Registry.CurrentUser.CreateSubKey("RegistryValueKindExample");
// Create name/value pairs.
// This overload supports QWord (long) values.
rk.SetValue("QuadWordValue", 42, RegistryValueKind.QWord);
// The following SetValue calls have the same effect as using the
// SetValue overload that does not specify RegistryValueKind.
//
rk.SetValue("DWordValue", 42, RegistryValueKind.DWord);
rk.SetValue("MultipleStringValue", new string[] {"One", "Two", "Three"}, RegistryValueKind.MultiString);
rk.SetValue("BinaryValue", new byte[] {10, 43, 44, 45, 14, 255}, RegistryValueKind.Binary);
rk.SetValue("StringValue", "The path is %PATH%", RegistryValueKind.String);
// This overload supports setting expandable string values. Compare
// the output from this value with the previous string value.
rk.SetValue("ExpandedStringValue", "The path is %PATH%", RegistryValueKind.ExpandString);
// Display all name/value pairs stored in the test key, with each
// registry data type in parentheses.
//
string[] valueNames = rk.GetValueNames();
foreach (string s in valueNames)
{
RegistryValueKind rvk = rk.GetValueKind(s);
switch (rvk)
{
case RegistryValueKind.MultiString :
string[] values = (string[]) rk.GetValue(s);
Console.Write("\r\n {0} ({1}) =", s, rvk);
for (int i = 0; i < values.Length; i++)
{
if (i != 0) Console.Write(",");
Console.Write(" \"{0}\"", values[i]);
}
Console.WriteLine();
break;
case RegistryValueKind.Binary :
byte[] bytes = (byte[]) rk.GetValue(s);
Console.Write("\r\n {0} ({1}) =", s, rvk);
for (int i = 0; i < bytes.Length; i++)
{
// Display each byte as two hexadecimal digits.
Console.Write(" {0:X2}", bytes[i]);
}
Console.WriteLine();
break;
default :
Console.WriteLine("\r\n {0} ({1}) = {2}", s, rvk, rk.GetValue(s));
break;
}
}
}
}
/*
This code example produces the following output:
QuadWordValue (QWord) = 42
DWordValue (DWord) = 42
MultipleStringValue (MultiString) =, "One", "Two", "Three"
BinaryValue (Binary) = 0A 2B 2C 2D 0E FF
StringValue (String) = The path is %PATH%
ExpandedStringValue (ExpandString) = The path is C:\Program Files\Microsoft.NET\SDK\v2.0\Bin;
[***The remainder of this output is omitted.***]
*/
Imports Microsoft.Win32
Public Class Example
Public Shared Sub Main()
' Delete and recreate the test key.
Registry.CurrentUser.DeleteSubKey("RegistryValueKindExample", False)
Dim rk As RegistryKey = Registry.CurrentUser.CreateSubKey("RegistryValueKindExample")
' Create name/value pairs.
' This overload supports QWord (long) values.
rk.SetValue("QuadWordValue", 42, RegistryValueKind.QWord)
' The following SetValue calls have the same effect as using the
' SetValue overload that does not specify RegistryValueKind.
'
rk.SetValue("DWordValue", 42, RegistryValueKind.DWord)
rk.SetValue("MultipleStringValue", New String() {"One", "Two", "Three"}, RegistryValueKind.MultiString)
rk.SetValue("BinaryValue", New Byte() {10, 43, 44, 45, 14, 255}, RegistryValueKind.Binary)
rk.SetValue("StringValue", "The path is %PATH%", RegistryValueKind.String)
' This overload supports setting expandable string values. Compare
' the output from this value with the previous string value.
rk.SetValue("ExpandedStringValue", "The path is %PATH%", RegistryValueKind.ExpandString)
' Display all name/value pairs stored in the test key, with each
' registry data type in parentheses.
'
Dim valueNames As String() = rk.GetValueNames()
Dim s As String
For Each s In valueNames
Dim rvk As RegistryValueKind = rk.GetValueKind(s)
Select Case rvk
Case RegistryValueKind.MultiString
Dim values As String() = CType(rk.GetValue(s), String())
Console.Write(vbCrLf & " {0} ({1}) =", s, rvk)
For i As Integer = 0 To values.Length - 1
If i <> 0 Then Console.Write(",")
Console.Write(" ""{0}""", values(i))
Next i
Console.WriteLine()
Case RegistryValueKind.Binary
Dim bytes As Byte() = CType(rk.GetValue(s), Byte())
Console.Write(vbCrLf & " {0} ({1}) =", s, rvk)
For i As Integer = 0 To bytes.Length - 1
' Display each byte as two hexadecimal digits.
Console.Write(" {0:X2}", bytes(i))
Next i
Console.WriteLine()
Case Else
Console.WriteLine(vbCrLf & " {0} ({1}) = {2}", s, rvk, rk.GetValue(s))
End Select
Next s
End Sub
End Class
'
'This code example produces the following output (some output is omitted):
'
' QuadWordValue (QWord) = 42
'
' DWordValue (DWord) = 42
'
' MultipleStringValue (MultiString) = "One", "Two", "Three"
'
' BinaryValue (Binary) = 0A 2B 2C 2D 0E FF
'
' StringValue (String) = The path is %PATH%
'
' ExpandedStringValue (ExpandString) = The path is C:\Program Files\Microsoft.NET\SDK\v2.0\Bin;
' [***The remainder of this output is omitted.***]
備註
注意
登錄機碼可以有一個未與任何名稱相關聯的值。 當登錄編輯器中顯示這個未命名的值時,會出現 「 (Default) 」 字串,而不是名稱。 若要擷取這個未命名的值,請為 指定 null
空字串 (“”“) name
。
GetValue當方法 () RegistryValueKind.ExpandString 擷取可展開的字串值時,它會使用本機環境的數據來擴充環境字串。 若要從遠端電腦上的登錄擷取可展開的字串值,請使用 GetValue(String, Object, RegistryValueOptions) 方法多載來指定您不想展開環境字串。
注意
如果包含環境變數可展開參考的值已儲存為字串 (RegistryValueKind.String) ,而不是 () 可展開的字串 RegistryValueKind.ExpandString , GetValue 則不會展開它。 呼叫 方法,即可在擷取 ExpandEnvironmentVariables 字串之後展開這類字串。
注意
從 PerformanceData 索引鍵擷取數據的建議方法是使用 PerformanceCounter 類別,而不是 RegistryKey.GetValue 方法。
GetValue 不支援讀取類型為 REG_NONE 或 REG_LINK的值。 在這兩種情況下,都會傳回預設值 (null
) ,而不是實際值。
另請參閱
適用於
GetValue(String, Object)
擷取與指定名稱關聯的值。 如果找不到名稱,則傳回您提供的預設值。
public:
System::Object ^ GetValue(System::String ^ name, System::Object ^ defaultValue);
public object GetValue (string name, object defaultValue);
public object? GetValue (string? name, object? defaultValue);
member this.GetValue : string * obj -> obj
Public Function GetValue (name As String, defaultValue As Object) As Object
參數
- name
- String
要擷取的值的名稱。 這個字串不會區分大小寫。
- defaultValue
- Object
name
不存在時所傳回的值。
傳回
與 name
關聯的值,擁有未展開的內嵌環境變數,如果找不到 defaultValue
,則為 name
。
例外狀況
使用者沒有讀取登錄機碼所需的使用權限。
包含指定值的 RegistryKey 已經關閉 (關閉的機碼無法存取)。
包含指定值的 RegistryKey 已標記為刪除。
使用者沒有必要的登錄權限。
範例
下列程式代碼範例會使用 值建立測試索引鍵,並擷取該值。 然後,此範例會嘗試從索引鍵擷取不存在的值;在此情況下, GetValue 方法會傳回指定的預設值。
using namespace System;
using namespace Microsoft::Win32;
public ref class RegGetDef
{
public:
static void Main()
{
// Create a reference to a valid key. In order for this code to
// work, the indicated key must have been created previously.
// The key name is not case-sensitive.
RegistryKey^ rk = Registry::LocalMachine->OpenSubKey("Software\\myTestKey", false);
// Get the value from the specified name/value pair in the key.
String^ valueName = "myTestValue";
Console::WriteLine("Retrieving registry value ...");
Console::WriteLine();
Object^ o = rk->GetValue(valueName);
Console::WriteLine("Object Type = " + o->GetType()->FullName);
Console::WriteLine();
switch (rk->GetValueKind(valueName))
{
case RegistryValueKind::String:
case RegistryValueKind::ExpandString:
Console::WriteLine("Value = " + o);
break;
case RegistryValueKind::Binary:
for each (Byte^ b in (array<Byte^>^)o)
{
Console::Write("{0:x2} ", b);
}
Console::WriteLine();
break;
case RegistryValueKind::DWord:
Console::WriteLine("Value = " + Convert::ToString((Int32^)o));
break;
case RegistryValueKind::QWord:
Console::WriteLine("Value = " + Convert::ToString((Int64^)o));
break;
case RegistryValueKind::MultiString:
for each (String^ s in (array<String^>^)o)
{
Console::Write("[{0:s}], ", s);
}
Console::WriteLine();
break;
default:
Console::WriteLine("Value = (Unknown)");
break;
}
// Attempt to retrieve a value that does not exist; the specified
// default value is returned.
String^ def = (String^)rk->GetValue("notavalue", "The default to return");
Console::WriteLine();
Console::WriteLine(def);
rk->Close();
}
};
int main()
{
RegGetDef::Main();
}
/*
Output:
Retrieving registry value ...
Object Type = System.String
Value = testData
The default to return
*/
using System;
using Microsoft.Win32;
class RegGetDef
{
public static void Main()
{
// Create a reference to a valid key. In order for this code to
// work, the indicated key must have been created previously.
// The key name is not case-sensitive.
RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\myTestKey", false);
// Get the value from the specified name/value pair in the key.
string valueName = "myTestValue";
Console.WriteLine("Retrieving registry value ...");
Console.WriteLine();
object o = rk.GetValue(valueName);
Console.WriteLine("Object Type = " + o.GetType().FullName);
Console.WriteLine();
switch (rk.GetValueKind(valueName))
{
case RegistryValueKind.String:
case RegistryValueKind.ExpandString:
Console.WriteLine("Value = " + o);
break;
case RegistryValueKind.Binary:
foreach (byte b in (byte[])o)
{
Console.Write("{0:x2} ", b);
}
Console.WriteLine();
break;
case RegistryValueKind.DWord:
Console.WriteLine("Value = " + Convert.ToString((int)o));
break;
case RegistryValueKind.QWord:
Console.WriteLine("Value = " + Convert.ToString((Int64)o));
break;
case RegistryValueKind.MultiString:
foreach (string s in (string[])o)
{
Console.Write("[{0:s}], ", s);
}
Console.WriteLine();
break;
default:
Console.WriteLine("Value = (Unknown)");
break;
}
// Attempt to retrieve a value that does not exist; the specified
// default value is returned.
string def = (string)rk.GetValue("notavalue", "The default to return");
Console.WriteLine();
Console.WriteLine(def);
rk.Close();
}
}
/*
Output:
Retrieving registry value ...
Object Type = System.String
Value = testData
The default to return
*/
Imports Microsoft.Win32
Public Class RegGetDef
Public Shared Sub Main()
' Create a reference to a valid key. In order for this code to
' work, the indicated key must have been created previously.
' The key name is not case-sensitive.
Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\myTestKey", false)
' Get the value from the specified name/value pair in the key.
Dim valueName As String = "myTestValue"
Console.WriteLine("Retrieving registry value ...")
Console.WriteLine()
Dim o As Object = rk.GetValue(valueName)
Console.WriteLine("Object Type = " + o.GetType().FullName)
Console.WriteLine()
Select Case rk.GetValueKind(valueName)
Case RegistryValueKind.String
Case RegistryValueKind.ExpandString
Console.WriteLine("Value = " + o)
Case RegistryValueKind.Binary
For Each b As Byte In CType(o,Byte())
Console.Write("{0:x2} ", b)
Next b
Console.WriteLine()
Case RegistryValueKind.DWord
Console.WriteLine("Value = " + Convert.ToString(CType(o,Int32)))
Case RegistryValueKind.QWord
Console.WriteLine("Value = " + Convert.ToString(CType(o,Int64)))
Case RegistryValueKind.MultiString
For Each s As String In CType(o,String())
Console.Write("[{0:s}], ", s)
Next s
Console.WriteLine()
Case Else
Console.WriteLine("Value = (Unknown)")
End Select
' Attempt to retrieve a value that does not exist; the specified
' default value is returned.
Dim Def As String = rk.GetValue("notavalue", "The default to return")
Console.WriteLine()
Console.WriteLine(def)
rk.Close()
End Sub
End Class
'
' Output:
' Retrieving registry value ...
'
' Object Type = System.String
'
' Value = testData
'
'The default to return
備註
使用此 多 GetValue 載來處理名稱尚不存在的情況,例如,第一次執行應用程式時。 每當您呼叫此多載時,請使用 defaultValue
參數來指定值,以在不存在時 name
傳回。
注意
登錄機碼可以有一個未與任何名稱相關聯的值。 當登錄編輯器中顯示這個未命名的值時,會出現 「 (Default) 」 字串,而不是名稱。 若要擷取這個未命名的值,請為 指定 null
空字串 (“”“) name
。
GetValue當方法 () RegistryValueKind.ExpandString 擷取可展開的字串值時,它會使用本機環境的數據來擴充環境字串。 若要從遠端電腦上的登錄擷取可展開的字串值,請使用 GetValue 多載來指定您不想展開環境字串。
注意
如果包含環境變數可展開參考的值已儲存為字串 () RegistryValueKind.String ,而不是可展開的字串 (RegistryValueKind.ExpandString) , GetValue 則方法不會展開它。 呼叫 方法,即可在擷取 ExpandEnvironmentVariables 字串之後展開這類字串。
注意
從 PerformanceData 索引鍵擷取數據的建議方法是使用 PerformanceCounter 類別,而不是 RegistryKey.GetValue 方法。
GetValue 不支援讀取類型為 REG_NONE 或 REG_LINK的值。 在這兩種情況下,都會傳回預設值 (null
) ,而不是實際值。