Export (0) Print
Expand All

RegistryKey.SetValue Method (String, Object)

Sets the specified name/value pair.

Namespace: Microsoft.Win32
Assembly: mscorlib (in mscorlib.dll)

public void SetValue (
	string name,
	Object value
)
public void SetValue (
	String name, 
	Object value
)
public function SetValue (
	name : String, 
	value : Object
)

Parameters

name

The name of the value to store.

value

The data to be stored.

Exception typeCondition

ArgumentNullException

value is a null reference (Nothing in Visual Basic).

ArgumentException

value is an unsupported data type.

-or-

name is longer than the maximum length allowed (255 characters).

ObjectDisposedException

The RegistryKey that contains the specified value is closed (closed keys cannot be accessed).

UnauthorizedAccessException

The RegistryKey is read-only, and cannot be written to; for example, the key has not been opened with write access.

-or-

The RegistryKey object represents a root-level node, and the operating system is Windows Millennium Edition or Windows 98.

SecurityException

The user does not have the permissions required to create or modify registry keys.

IOException

The RegistryKey object represents a root-level node, and the operating system is Windows 2000, Windows XP, or Windows Server 2003.

Because many values can be stored in each key in the registry, you must use the name parameter to specify the particular value you want to set.

NoteNote

A registry key can have one value that is not associated with any name. When this unnamed value is displayed in the registry editor, the string "(Default)" appears instead of a name. To set this unnamed value, specify either a null reference (Nothing in Visual Basic) or the empty string ("") for name.

In order to set values in a key, you must open the key with write access. After you have opened a key with write access, you can change any of the name/value pairs in that key.

If the specified name does not exist in the key, it is created and the associated value is set to value.

This overload of SetValue stores 64-bit integers as strings (RegistryValueKind.String). To store 64-bit numbers as RegistryValueKind.QWord values, use the SetValue(String,Object,RegistryValueKind) overload that specifies RegistryValueKind.

This overload of SetValue stores all string values as RegistryValueKind.String, even if they contain expandable references to environment variables. To save string values as expandable strings (RegistryValueKind.ExpandString), use the SetValue(String,Object,RegistryValueKind) overload that specifies RegistryValueKind.

Numeric types other than 32-bit integers are stored as strings by this method overload. Enumeration elements are stored as strings containing the element names.

Caution noteCaution

Do not expose RegistryKey objects in such a way that a malicious program could create thousands of meaningless subkeys or key/value pairs. For example, do not allow callers to enter arbitrary keys or values.

NoteNote

On Windows 98 and Windows Millennium Edition the registry is not Unicode, and not all Unicode characters are valid for all code pages. A Unicode character that is invalid for the current code page is replaced by the best available match. No exception is thrown.

The following code example shows how the SetValue method determines the registry data type when it sets values. The example creates a test key and adds values of different data types to the key. The example then reads the name/value pairs and displays them to the console, using the GetValueKind method to display the corresponding registry data types.

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;
            }
        }
    }
}

import System.*;
import Microsoft.Win32.*;

public class Example
{
    public static void main(String[] args)
    {
        // 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", System.Convert.ToUInt64(42));
        rk.SetValue("LargeNumberValue2", System.Convert.ToUInt64(42000000000L));
        
        rk.SetValue("DWordValue", (UInt32)42, RegistryValueKind.DWord);
        rk.SetValue("MultipleStringValue", new String[] {"One", "Two", "Three"});
        rk.SetValue("BinaryValue", new ubyte[] {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();

        for (int iCtr1 = 0; iCtr1 < valueNames.get_Length(); iCtr1++) {
            String s = valueNames [iCtr1];
            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.get_Item(0));
                    for (int i = 1; i < values.get_Length(); i++) {
                        Console.Write(", \"{0}\"", values.get_Item(i));
                    }
                    Console.WriteLine();
                    break;

                case RegistryValueKind.Binary:
                    ubyte bytes[] = (ubyte[])(rk.GetValue(s));
                    Console.Write("\r\n {0} ({1}) = {2:X2}", s, rvk,
                        bytes.get_Item(0));
                    for (int i = 1; i < bytes.get_Length(); i++) {
                        // Display each byte as two hexadecimal digits.
                        Console.Write(" {0:X2}", bytes.get_Item(i));
                    }
                    Console.WriteLine();
                    break;

                default:
                    Console.WriteLine("\r\n {0} ({1}) = {2}", s, rvk, 
                        rk.GetValue(s));
                    break;
            }
        }
    } //main
} //Example

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2015 Microsoft