This documentation is archived and is not being maintained.

Registry.SetValue Method (String, String, Object, RegistryValueKind)

Sets the name/value pair on the specified registry key, using the specified registry data type. If the specified key does not exist, it is created.

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

public static void SetValue(
	string keyName,
	string valueName,
	Object value,
	RegistryValueKind valueKind


Type: System.String

The full registry path of the key, beginning with a valid registry root, such as "HKEY_CURRENT_USER".

Type: System.String

The name of the name/value pair.

Type: System.Object

The value to be stored.

Type: Microsoft.Win32.RegistryValueKind

The registry data type to use when storing the data.


value is null.


keyName does not begin with a valid registry root.


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


The type of value did not match the registry data type specified by valueKind, therefore the data could not be converted properly.


The RegistryKey is read-only, and thus cannot be written to; for example, it is a root-level node, or the key has not been opened with write access.


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

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


A registry key can contain 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 null or the empty string ("") for valueName.

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

If keyName specifies a subkey that does not exist, the subkey is created in the specified root. For example, in Visual Basic the string "HKEY_CURRENT_USER\MyTestKey" creates the subkey "MyTestKey" in the HKEY_CURRENT_USER root. The string "HKEY_CURRENT_USER\MyTestKey\Key2\Key3" creates the nested subkeys "MyTestKey", "MyTestKey\Key2", and "MyTestKey\Key2\Key3".



The SetValue method opens a registry key, sets the value, and closes the key each time it is called. If you need to modify a large number of values, the RegistryKey.SetValue method might provide better performance. The RegistryKey class also provides methods that allow you to add an access control list (ACL) to a registry key, to test the data type of a value before retrieving it, and to delete keys.

If the type of the specified value does not match the specified valueKind, and the data cannot be converted, ArgumentException is thrown. For example, you can store a System.Int64 as a RegistryValueKind.DWord, but only if its value is less than the maximum value of a System.Int32. You cannot store a single string value as a RegistryValueKind.MultiString.


If boxed values are passed for RegistryValueKind.DWord or RegistryValueKind.QWord, the conversion is done using the invariant culture.


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 stores values of several data types in an example key, creating the key as it does so, and then retrieves and displays the values. The example demonstrates storing and retrieving the default (nameless) name/value pair, and the use of defaultValue when a name/value pair does not exist.

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,

        // 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%",

        // 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, 
            "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",
        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,
            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,
             "Default if TestExpand does not exist.");
        Console.WriteLine("TestExpand: {0}", tExpand);

        // A string stored as ExpandString is expanded.
        string tExpand2 = (string) Registry.GetValue(keyName,
            "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.");
// 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.

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

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0