Microsoft.Win32.RegistryKey.SetValue Method

Sets the specified name/value pair.

Syntax

public void SetValue (string name, object value)

Parameters

name
The name of the value to store.
value
The data to be stored.

Remarks

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.

Note:

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.

This overload of erload:Microsoft.Win32.RegistryKey.SetValue stores 64-bit integers as strings (RegistryValueKind.String). To store 64-bit numbers as RegistryValueKind.QWord values, use the RegistryKey.SetValue(string, object, RegistryValueKind) overload that specifies Microsoft.Win32.RegistryValueKind.

This overload of erload:Microsoft.Win32.RegistryKey.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 RegistryKey.SetValue(string, object, RegistryValueKind) overload that specifies Microsoft.Win32.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.

Note:

Do not expose Microsoft.Win32.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.

Note:

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.

Requirements

Namespace: Microsoft.Win32
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0