RegistryKey.SetValue Method (String, Object, RegistryValueKind)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Sets the value of a name/value pair in the registry key, using the specified registry data type.

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

public void SetValue(
	string name,
	object value,
	RegistryValueKind valueKind


Type: System.String

The name of the value to be stored.

Type: System.Object

The data to be stored.

Type: Microsoft.Win32.RegistryValueKind

The registry data type to use when storing the data.

Exception Condition

value is null.


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


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


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


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


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


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.


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 null 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.


Specifying the registry data type Unknown is the same as using the SetValue overload.

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.


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.


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 creates a test key and uses the SetValue method to store several values, specifying the registry data type for each value. 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("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]);

                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]);

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

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.***]



to modify the specified registry key if it exists, or to create the registry key if it does not already exist. Associated enumerations: RegistryPermissionAccess.Write, RegistryPermissionAccess.Create


for the ability to access the specified registry key if it is a remote key. Associated enumeration: SecurityPermissionFlag.UnmanagedCode

.NET Framework
Available since 2.0
Return to top