Microsoft.Win32.RegistryKey.SetValue Method

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

Syntax

[System.Runtime.InteropServices.ComVisible(false)]
public void SetValue (string name, object value, RegistryValueKind valueKind)

Parameters

name
The name of the value to be stored.
value
The data to be stored.
valueKind
The registry data type to use when storing the data.

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.

Note:

Specifying the registry data type RegistryValueKind.Unknown is the same as using the RegistryKey.SetValue(string, object) 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 long as a RegistryValueKind.DWord, but only if its value is less than the maximum value of a int. You cannot store a single string value as a RegistryValueKind.MultiString.

Note:

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

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: 2.0.0.0, 4.0.0.0
Since: .NET 2.0