RegistryValueKind 列舉
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
設定將資料值放到登錄內時所使用的資料型別,或代表登錄資料庫內資料值的型別。
public enum class RegistryValueKind
public enum RegistryValueKind
[System.Runtime.InteropServices.ComVisible(true)]
public enum RegistryValueKind
type RegistryValueKind =
[<System.Runtime.InteropServices.ComVisible(true)>]
type RegistryValueKind =
Public Enum RegistryValueKind
- 繼承
- 屬性
欄位
Binary | 3 | 任何形式的二進位資料, 此值相當於 Windows API 中的登錄資料類型 REG_BINARY。 |
DWord | 4 | 32 位元二進位數字, 此值相當於 Windows API 中的登錄資料類型 REG_DWORD。 |
ExpandString | 2 | 以 null 結尾的字串,其中包含未展開的環境變數參考,例如 %PATH%,以後要擷取值時,環境變數就會展開。 此值相當於 Windows API 中的登錄資料類型 REG_EXPAND_SZ。 |
MultiString | 7 | 以 null 結尾的字串所組成的陣列,整個陣列最後以兩個 null 字元做為結尾。 此值相當於 Windows API 中的登錄資料類型 REG_MULTI_SZ。 |
None | -1 | 無資料類型。 |
QWord | 11 | 64 位元二進位數字, 此值相當於 Windows API 中的登錄資料類型 REG_QWORD。 |
String | 1 | null 終止的字串。 此值相當於 Windows API 中的登錄資料類型 REG_SZ。 |
Unknown | 0 | 不支援的登錄資料型別, 例如,Microsoft Windows API 登錄資料類型 REG_RESOURCE_LIST 就不受支援。 您可以使用這個值指定 SetValue(String, Object) 方法在儲存名稱/值組時,應該自行決定適當的登錄資料型別。 |
範例
下列程式代碼範例會建立登錄機碼,並使用 指定登錄數據類型來 RegistryValueKind 設定該機碼的數個值。 然後,此範例會使用 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;
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 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.***]
備註
列舉 RegistryValueKind 會定義支援的登錄數據類型集,以及用於不支援類型的值, (未知) 。 從 .NET Framework 4 開始,您可以指定不要搭配 None 值使用資料類型。
RegistryKey.GetValueKind使用 方法,在擷取值之前先判斷登錄機碼值的數據類型。 當您設定登錄機碼值時,請使用 SetValue 方法來明確指定登錄數據類型。