Export (0) Print
Expand All

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 a null reference (Nothing in Visual Basic) if the specified key does not exist.

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

public static function GetValue(
	keyName : String, 
	valueName : String, 
	defaultValue : Object
) : Object

Parameters

keyName
Type: System.String

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

valueName
Type: System.String

The name of the name/value pair.

defaultValue
Type: System.Object

The value to return if valueName does not exist.

Return Value

Type: System.Object
a null reference (Nothing in Visual Basic) if the subkey specified by keyName does not exist; otherwise, the value associated with valueName, or defaultValue if valueName is not found.

ExceptionCondition
SecurityException

The user does not have the permissions required to read from the registry key.

IOException

The RegistryKey that contains the specified value has been marked for deletion.

ArgumentException

keyName does not begin with a valid registry root.

The string valueName is not case-sensitive.

NoteNote:

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 a null reference (Nothing in Visual Basic) 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 GetValue 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), GetValue does not expand it. You can expand such a string after it has been retrieved by calling the Environment.ExpandEnvironmentVariables method.

NoteNote:

The recommended way to retrieve data from HKEY_PERFORMANCE_DATA is to use the PerformanceCounter class rather than the RegistryKey.GetValue method.

The GetValue and SetValue methods open and close registry keys each time they are used, so they do not perform as well as the methods of the RegistryKey class if you access a large number of values.

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.

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.

No code example is currently available or this language may not be supported.

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

Community Additions

ADD
Show:
© 2014 Microsoft