Microsoft.Win32.Registry.GetValue Method

Retrieves the value associated with the specified name, in the specified registry key. If the name is not found in the specified key, returns a default value that you provide, or null if the specified key does not exist.

Syntax

public static object GetValue (string keyName, string valueName, object defaultValue)

Parameters

keyName
The full registry path of the key, beginning with a valid registry root, such as "HKEY_CURRENT_USER".
valueName
The name of the name/value pair.
defaultValue
The value to return if valueName does not exist.

Returns

null if the subkey specified by keyName does not exist; otherwise, the value associated with valueName, or defaultValue if valueName is not found.

Remarks

The string valueName is not case-sensitive.

Note:

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 retrieve this unnamed value, specify either null or the empty string ("") for valuName.

Valid root names are HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_CLASSES_ROOT, HKEY_USERS, HKEY_PERFORMANCE_DATA, HKEY_CURRENT_CONFIG, and HKEY_DYN_DATA. For example, in Visual Basic the string "HKEY_CURRENT_USER\MyTestKey" accesses key/value pairs for the subkey "MyTestKey" in the HKEY_CURRENT_USER root.

When the RegistryKey.GetValue(string) method retrieves expandable string values (RegistryValueKind.ExpandString), it expands environment strings using data from the local environment. If a value containing expandable references to environment variables has been stored as a string (RegistryValueKind.String), rather than as an expandable string (RegistryValueKind.ExpandString), RegistryKey.GetValue(string) does not expand it. You can expand such a string after it has been retrieved by calling the Environment.ExpandEnvironmentVariables(string) method.

Note:

The recommended way to retrieve data from HKEY_PERFORMANCE_DATA is to use the System.Diagnostics.PerformanceCounter class rather than the erload:Microsoft.Win32.RegistryKey.GetValue method.

The Registry.GetValue(string, string, object) and erload:Microsoft.Win32.Registry.SetValue methods open and close registry keys each time they are used, so they do not perform as well as the methods of the Microsoft.Win32.RegistryKey class if you access a large number of values.

Microsoft.Win32.RegistryKey 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.

Requirements

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