Registry.SetValue 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
設定登錄機碼中名稱/值組的值。
多載
SetValue(String, String, Object) |
在指定的登錄機碼上設定指定的名稱/值組。 如果指定的機碼不存在,則會建立它。 |
SetValue(String, String, Object, RegistryValueKind) |
使用指定的登錄資料型別,在指定的登錄機碼上設定名稱/值組。 如果指定的機碼不存在,則會建立它。 |
範例
下列程式代碼範例會將數個數據類型的值儲存在範例索引鍵中,建立索引鍵的方式,然後擷取並顯示值。 此範例示範儲存和擷取預設 (無名稱) 名稱/值組,以及在名稱/值組不存在時使用 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.
SetValue(String, String, Object)
- 來源:
- Registry.cs
在指定的登錄機碼上設定指定的名稱/值組。 如果指定的機碼不存在,則會建立它。
public:
static void SetValue(System::String ^ keyName, System::String ^ valueName, System::Object ^ value);
public static void SetValue (string keyName, string valueName, object value);
public static void SetValue (string keyName, string? valueName, object value);
static member SetValue : string * string * obj -> unit
Public Shared Sub SetValue (keyName As String, valueName As String, value As Object)
參數
- keyName
- String
機碼的完整登錄路徑,以有效登錄根目錄開頭 (例如 "HKEY_CURRENT_USER")。
- valueName
- String
名稱/值組的名稱。
- value
- Object
要儲存的值。
例外狀況
value
為 null
。
RegistryKey 為唯讀,因此無法寫入 (例如,它是根層次節點)。
使用者沒有建立或修改登錄機碼所需的使用權限。
範例
下列程式代碼範例會將數個數據類型的值儲存在範例索引鍵中,建立索引鍵的方式,然後擷取並顯示值。 此範例示範儲存和擷取預設 (無名稱) 名稱/值組,以及在名稱/值組不存在時使用 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.
備註
從 .NET Framework 4 開始,valueName
參數不再限製為最多 255 個字元;不過,keyName
參數會繼續有 255 個字元的限制。
因為許多值都可以儲存在登錄中的每個機碼中,所以您必須使用 valueName
參數來指定您想要設定的特定值。
注意
登錄機碼可以包含一個未與任何名稱相關聯的值。 當登錄編輯器中顯示這個未命名的值時,會出現 「 (Default) 」 字串,而不是名稱。 若要設定這個未命名的值,請針對 指定 null
或空字串 (“”“) valueName
。
如果 valueName
索引鍵中不存在,則會建立索引鍵,並將相關聯的值設定為 value
。
如果 keyName
指定不存在的子機碼,則會在指定的根目錄中建立子機碼。 例如,在 Visual Basic 中,字串 「HKEY_CURRENT_USER\MyTestKey」 會在HKEY_CURRENT_USER根目錄中建立子機碼 「MyTestKey」。 字串 “HKEY_CURRENT_USER\MyTestKey\Key2\Key3” 會建立巢狀子機碼 “MyTestKey”、“MyTestKey\Key2” 和 “MyTestKey\Key2\Key3”。
有效的根名稱包括HKEY_CURRENT_USER、HKEY_LOCAL_MACHINE、HKEY_CLASSES_ROOT、HKEY_USERS、HKEY_PERFORMANCE_DATA、HKEY_CURRENT_CONFIG和HKEY_DYN_DATA。
注意
方法 SetValue 會開啟登錄機碼、設定值,並在每次呼叫時關閉密鑰。 如果您需要修改大量值,方法 RegistryKey.SetValue 可能會提供更好的效能。 類別 RegistryKey 也提供方法,可讓您將訪問控制清單 (ACL) 新增至登錄機碼、在擷取值之前測試值的數據類型,以及刪除機碼。
這個 多 SetValue 載會將 64 位整數儲存為字串, RegistryValueKind.String () 。 若要將 64 位數位儲存為 RegistryValueKind.QWord 值,請使用 SetValue(String, String, Object, RegistryValueKind) 方法多載。
這個 多 SetValue 載會將所有字串值儲存為 RegistryValueKind.String 物件,即使它們包含環境變數的可展開參考也一樣。 若要將字串值儲存為可展開的字串 (RegistryValueKind.ExpandString) ,請使用 SetValue(String, String, Object, RegistryValueKind) 方法多載。
這個多載相當於使用RegistryValueKind.Unknown呼叫 SetValue(String, String, Object, RegistryValueKind) 方法多載。
適用於
SetValue(String, String, Object, RegistryValueKind)
- 來源:
- Registry.cs
使用指定的登錄資料型別,在指定的登錄機碼上設定名稱/值組。 如果指定的機碼不存在,則會建立它。
public:
static void SetValue(System::String ^ keyName, System::String ^ valueName, System::Object ^ value, Microsoft::Win32::RegistryValueKind valueKind);
public static void SetValue (string keyName, string valueName, object value, Microsoft.Win32.RegistryValueKind valueKind);
public static void SetValue (string keyName, string? valueName, object value, Microsoft.Win32.RegistryValueKind valueKind);
static member SetValue : string * string * obj * Microsoft.Win32.RegistryValueKind -> unit
Public Shared Sub SetValue (keyName As String, valueName As String, value As Object, valueKind As RegistryValueKind)
參數
- keyName
- String
機碼的完整登錄路徑,以有效登錄根目錄開頭 (例如 "HKEY_CURRENT_USER")。
- valueName
- String
名稱/值組的名稱。
- value
- Object
要儲存的值。
- valueKind
- RegistryValueKind
儲存資料時要使用的登錄資料類型。
例外狀況
value
為 null
。
keyName
不是以有效的登錄根目錄開頭。
-或-
keyName
超過允許的最大長度 (255 個字元)。
-或-
value
的型別與 valueKind
所指定的登錄資料型別不符,因此,無法正確轉換資料。
RegistryKey 為唯讀,因此無法寫入 (例如,它是根層次節點,或未以寫入權限來開啟該機碼)。
使用者沒有建立或修改登錄機碼所需的使用權限。
範例
下列程式代碼範例會將數個數據類型的值儲存在範例索引鍵中,建立索引鍵的方式,然後擷取並顯示值。 此範例示範儲存和擷取預設 (無名稱) 名稱/值組,以及在名稱/值組不存在時使用 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.
備註
從 .NET Framework 4 開始,valueName
參數不再限製為最多 255 個字元;不過,keyName
參數會繼續有 255 個字元的限制。
因為許多值都可以儲存在登錄中的每個機碼中,所以您必須使用 valueName
參數來指定您想要設定的特定值。
注意
登錄機碼可以包含一個未與任何名稱相關聯的值。 當登錄編輯器中顯示這個未命名的值時,會出現 「 (Default) 」 字串,而不是名稱。 若要設定這個未命名的值,請針對 指定 null
或空字串 (“”“) valueName
。
如果 valueName
索引鍵中不存在,則會建立索引鍵,並將相關聯的值設定為 value
。
如果 keyName
指定不存在的子機碼,則會在指定的根目錄中建立子機碼。 例如,在 Visual Basic 中,字串 「HKEY_CURRENT_USER\MyTestKey」 會在HKEY_CURRENT_USER根目錄中建立子機碼 「MyTestKey」。 字串 “HKEY_CURRENT_USER\MyTestKey\Key2\Key3” 會建立巢狀子機碼 “MyTestKey”、“MyTestKey\Key2” 和 “MyTestKey\Key2\Key3”。
有效的根名稱包括HKEY_CURRENT_USER、HKEY_LOCAL_MACHINE、HKEY_CLASSES_ROOT、HKEY_USERS、HKEY_PERFORMANCE_DATA、HKEY_CURRENT_CONFIG和HKEY_DYN_DATA。
注意
方法 SetValue 會開啟登錄機碼、設定值,並在每次呼叫時關閉密鑰。 如果您需要修改大量值,方法 RegistryKey.SetValue 可能會提供更好的效能。 類別 RegistryKey 也提供方法,可讓您將訪問控制清單 (ACL) 新增至登錄機碼、在擷取值之前測試值的數據類型,以及刪除機碼。
如果指定的 value
型別不符合指定的 valueKind
,而且無法轉換資料, ArgumentException 則會擲回 。 例如,您可以將 儲存 System.Int64 為 RegistryValueKind.DWord,但只有當其值小於 的 System.Int32最大值時。 您無法儲存單一字串值儲存為 RegistryValueKind.MultiString。
注意
如果 針對 或 RegistryValueKind.QWord傳遞 RegistryValueKind.DWord Boxed 值,則會使用不因文化特性而異而完成轉換。