RegistryKey.SetValue Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Kayıt defteri anahtarındaki bir ad/değer çiftinin değerini ayarlar. Aşırı yüklemeye bağlı olarak, kayıt defteri veri türü depolanmakta olan veri türünden veya belirtilen RegistryValueKindbir veri türünden belirlenir.
Aşırı Yüklemeler
SetValue(String, Object) |
Belirtilen ad/değer çiftini ayarlar. |
SetValue(String, Object, RegistryValueKind) |
Belirtilen kayıt defteri veri türünü kullanarak kayıt defteri anahtarındaki bir ad/değer çiftinin değerini ayarlar. |
SetValue(String, Object)
- Kaynak:
- RegistryKey.cs
Belirtilen ad/değer çiftini ayarlar.
public:
void SetValue(System::String ^ name, System::Object ^ value);
public void SetValue (string name, object value);
public void SetValue (string? name, object value);
member this.SetValue : string * obj -> unit
Public Sub SetValue (name As String, value As Object)
Parametreler
- name
- String
Depoacak değerin adı.
- value
- Object
Depolanacak veriler.
Özel durumlar
value
, null
değeridir.
value
desteklenmeyen bir veri türüdür.
RegistryKey Belirtilen değeri içeren kapalı (kapalı anahtarlara erişilemiyor).
RegistryKey salt okunurdur ve yazılamaz; örneğin, anahtar yazma erişimiyle açılmamıştır.
Kullanıcının kayıt defteri anahtarlarını oluşturmak veya değiştirmek için gereken izinleri yok.
RegistryKey nesnesi kök düzeyinde bir düğümü temsil eder ve işletim sistemi Windows 2000, Windows XP veya Windows Server 2003'dür.
Örnekler
Aşağıdaki kod örneği, yöntemin SetValue değerleri ayarlarken kayıt defteri veri türünü nasıl belirlediğini gösterir. Örnek bir test anahtarı oluşturur ve anahtara farklı veri türlerinin değerlerini ekler. Örnek daha sonra ad/değer çiftlerini okur ve karşılık gelen kayıt defteri veri türlerini görüntülemek için yöntemini kullanarak GetValueKind bunları konsolda görüntüler.
using namespace System;
using namespace Microsoft::Win32;
int main()
{
// Delete and recreate the test key.
Registry::CurrentUser->DeleteSubKey( "RegistrySetValueExample", false );
RegistryKey ^ rk = Registry::CurrentUser->CreateSubKey( "RegistrySetValueExample" );
// Create name/value pairs.
// Numeric values that cannot be interpreted as DWord (int) values
// are stored as strings.
rk->SetValue( "LargeNumberValue1", (long)42 );
rk->SetValue( "LargeNumberValue2", 42000000000 );
rk->SetValue( "DWordValue", 42 );
array<String^>^temp0 = {"One","Two","Three"};
rk->SetValue( "MultipleStringValue", temp0 );
array<Byte>^temp1 = {10,43,44,45,14,255};
rk->SetValue( "BinaryValue", temp1 );
// This overload of SetValue does not support expanding strings. Use
// the overload that allows you to specify RegistryValueKind.
rk->SetValue( "StringValue", "The path is %PATH%" );
// 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}) = \"{2}\"", s, rvk, values[ 0 ] );
for ( int i = 1; i < values->Length; i++ )
{
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}) = {2:X2}", s, rvk, bytes[ 0 ] );
for ( int i = 1; 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;
}
}
}
using System;
using Microsoft.Win32;
public class Example
{
public static void Main()
{
// Delete and recreate the test key.
Registry.CurrentUser.DeleteSubKey("RegistrySetValueExample", false);
RegistryKey rk = Registry.CurrentUser.CreateSubKey("RegistrySetValueExample");
// Create name/value pairs.
// Numeric values that cannot be interpreted as DWord (int) values
// are stored as strings.
rk.SetValue("LargeNumberValue1", (long) 42);
rk.SetValue("LargeNumberValue2", 42000000000);
rk.SetValue("DWordValue", 42);
rk.SetValue("MultipleStringValue", new string[] {"One", "Two", "Three"});
rk.SetValue("BinaryValue", new byte[] {10, 43, 44, 45, 14, 255});
// This overload of SetValue does not support expanding strings. Use
// the overload that allows you to specify RegistryValueKind.
rk.SetValue("StringValue", "The path is %PATH%");
// 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}) = \"{2}\"", s, rvk, values[0]);
for (int i = 1; i < values.Length; i++)
{
Console.Write(", \"{0}\"", values[i]);
}
Console.WriteLine();
break;
case RegistryValueKind.Binary :
byte[] bytes = (byte[]) rk.GetValue(s);
Console.Write("\r\n {0} ({1}) = {2:X2}", s, rvk, bytes[0]);
for (int i = 1; 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;
}
}
}
}
Imports Microsoft.Win32
Public Class Example
Public Shared Sub Main()
' Delete and recreate the test key.
Registry.CurrentUser.DeleteSubKey("RegistrySetValueExample", False)
Dim rk As RegistryKey = Registry.CurrentUser.CreateSubKey("RegistrySetValueExample")
' Create name/value pairs.
' Numeric values that cannot be interpreted as DWord (int) values
' are stored as strings.
rk.SetValue("LargeNumberValue1", CType(42, Long))
rk.SetValue("LargeNumberValue2", 42000000000)
rk.SetValue("DWordValue", 42)
rk.SetValue("MultipleStringValue", New String() {"One", "Two", "Three"})
rk.SetValue("BinaryValue", New Byte() {10, 43, 44, 45, 14, 255})
' This overload of SetValue does not support expanding strings. Use
' the overload that allows you to specify RegistryValueKind.
rk.SetValue("StringValue", "The path is %PATH%")
' 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}) = ""{2}""", s, rvk, values(0))
Dim i As Integer
For i = 1 To values.Length - 1
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}) = {2:X2}", s, rvk, bytes(0))
Dim i As Integer
For i = 1 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
Açıklamalar
Kayıt defterindeki her anahtarda birçok değer depolanabileceğinden, ayarlamak istediğiniz değeri belirtmek için parametresini kullanmanız name
gerekir.
Not
Bir kayıt defteri anahtarı herhangi bir ad ile ilişkili olmayan bir değere sahip olabilir. Bu adlandırılmamış değer, kayıt defteri düzenleyicisinde görüntülendiğinde, bir ad yerine "(varsayılan)" dizesi görüntülenir. Bu adlandırılmamış değeri ayarlamak için name
için veya boş dizeyi ("") belirtinnull
.
Anahtardaki değerleri ayarlamak için anahtarı yazma erişimiyle açmanız gerekir. Yazma erişimi olan bir anahtarı açtıktan sonra, bu anahtardaki ad/değer çiftlerinden herhangi birini değiştirebilirsiniz.
Belirtilen name
anahtarda yoksa oluşturulur ve ilişkili değer olarak value
ayarlanır.
Bu aşırı yükleme SetValue , 64 bit tamsayıları dize (RegistryValueKind.String) olarak depolar. 64 bit sayıları değer olarak RegistryValueKind.QWord depolamak için öğesini belirten RegistryValueKindaşırı yüklemeyi kullanınSetValue(String, Object, RegistryValueKind).
Bu aşırı yükleme, SetValue ortam değişkenlerine genişletilebilir başvurular içerseler bile tüm dize değerlerini olarak RegistryValueKind.Stringdepolar. Dize değerlerini genişletilebilir dizeler ()RegistryValueKind.ExpandString olarak kaydetmek için öğesini belirten RegistryValueKindaşırı yüklemeyi kullanınSetValue(String, Object, RegistryValueKind).
32 bit tamsayılar dışındaki sayısal türler, bu yöntem aşırı yüklemesi tarafından dize olarak depolanır. Numaralandırma öğeleri, öğe adlarını içeren dizeler olarak depolanır.
Dikkat
Kötü amaçlı bir programın binlerce anlamsız alt anahtar veya anahtar/değer çifti oluşturabileceği şekilde nesneleri kullanıma sunma RegistryKey . Örneğin, çağrı yapanların rasgele anahtarlar veya değerler girmesine izin vermeyin.
Ayrıca bkz.
Şunlara uygulanır
SetValue(String, Object, RegistryValueKind)
- Kaynak:
- RegistryKey.cs
Belirtilen kayıt defteri veri türünü kullanarak kayıt defteri anahtarındaki bir ad/değer çiftinin değerini ayarlar.
public:
void SetValue(System::String ^ name, System::Object ^ value, Microsoft::Win32::RegistryValueKind valueKind);
public void SetValue (string name, object value, Microsoft.Win32.RegistryValueKind valueKind);
public void SetValue (string? name, object value, Microsoft.Win32.RegistryValueKind valueKind);
[System.Runtime.InteropServices.ComVisible(false)]
public void SetValue (string name, object value, Microsoft.Win32.RegistryValueKind valueKind);
member this.SetValue : string * obj * Microsoft.Win32.RegistryValueKind -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.SetValue : string * obj * Microsoft.Win32.RegistryValueKind -> unit
Public Sub SetValue (name As String, value As Object, valueKind As RegistryValueKind)
Parametreler
- name
- String
Depolanacak değerin adı.
- value
- Object
Depolanacak veriler.
- valueKind
- RegistryValueKind
Verileri depolarken kullanılacak kayıt defteri veri türü.
- Öznitelikler
Özel durumlar
value
, null
değeridir.
türü value
tarafından belirtilen kayıt defteri veri türüyle valueKind
eşleşmediğinden veriler düzgün bir şekilde dönüştürülemedi.
RegistryKey Belirtilen değeri içeren kapalı (kapalı anahtarlara erişilemiyor).
RegistryKey salt okunurdur ve yazılamaz; örneğin, anahtar yazma erişimiyle açılmamıştır.
Kullanıcının kayıt defteri anahtarlarını oluşturmak veya değiştirmek için gereken izinleri yok.
RegistryKey nesnesi kök düzeyinde bir düğümü temsil eder ve işletim sistemi Windows 2000, Windows XP veya Windows Server 2003'dür.
Örnekler
Aşağıdaki kod örneği bir test anahtarı oluşturur ve her değer için kayıt defteri veri türünü belirterek çeşitli değerleri depolamak için yöntemini kullanır SetValue . Örnek daha sonra ad/değer çiftlerini okur ve karşılık gelen kayıt defteri veri türlerini görüntülemek için yöntemini kullanarak GetValueKind bunları konsolda görüntüler.
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.***]
Açıklamalar
Kayıt defterindeki her anahtarda birçok değer depolanabileceğinden, ayarlamak istediğiniz değeri belirtmek için parametresini kullanmanız name
gerekir.
Not
Bir kayıt defteri anahtarı herhangi bir ad ile ilişkili olmayan bir değere sahip olabilir. Bu adlandırılmamış değer, kayıt defteri düzenleyicisinde görüntülendiğinde, bir ad yerine "(varsayılan)" dizesi görüntülenir. Bu adlandırılmamış değeri ayarlamak için name
için veya boş dizeyi ("") belirtinnull
.
Anahtardaki değerleri ayarlamak için anahtarı yazma erişimiyle açmanız gerekir. Yazma erişimi olan bir anahtarı açtıktan sonra, bu anahtardaki ad/değer çiftlerinden herhangi birini değiştirebilirsiniz.
Belirtilen name
anahtarda yoksa oluşturulur ve ilişkili değer olarak value
ayarlanır.
Belirtilen türü belirtilen value
valueKind
ile eşleşmiyorsa ve veriler dönüştürülemiyorsa oluşturulur ArgumentException . Örneğin, bir System.Int64 değerini olarak RegistryValueKind.DWorddepolayabilirsiniz, ancak yalnızca değeri bir değerinin en büyük değerinden System.Int32küçükse. Tek bir dize değerini olarak RegistryValueKind.MultiStringdepolayamazsınız.
Not
veya RegistryValueKind.QWordiçin RegistryValueKind.DWord kutulanmış değerler geçirilirse dönüştürme sabit kültür kullanılarak yapılır.
Dikkat
Kötü amaçlı bir programın binlerce anlamsız alt anahtar veya anahtar/değer çifti oluşturabileceği şekilde nesneleri kullanıma sunma RegistryKey . Örneğin, çağrı yapanların rasgele anahtarlar veya değerler girmesine izin vermeyin.