Export (0) Print
Expand All

RegistryKey.GetValue Method (String, Object)

Retrieves the value associated with the specified name. If the name is not found, returns the default value that you provide.

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

public Object GetValue (
	string name,
	Object defaultValue
)
public Object GetValue (
	String name, 
	Object defaultValue
)
public function GetValue (
	name : String, 
	defaultValue : Object
) : Object

Parameters

name

The name of the value to retrieve.

defaultValue

The value to return if name does not exist.

Return Value

The value associated with name, with any embedded environment variables left unexpanded, or defaultValue if name is not found.

Exception typeCondition

SecurityException

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

ObjectDisposedException

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

IOException

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

UnauthorizedAccessException

The user does not have the necessary registry rights.

Use this overload of GetValue to handle the case where a name does not exist yet — for example, the first time your application is run. Whenever you call this overload, use the defaultValue parameter to specify the value to return if name does not exist.

The string name is not case-sensitive.

NoteNote

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 retrieve this unnamed value, specify either a null reference (Nothing in Visual Basic) or the empty string ("") for name.

When the GetValue method retrieves expandable string values (RegistryValueKind.ExpandString), it expands environment strings using data from the local environment. To retrieve expandable string values from the registry on a remote computer, use the GetValue overload to specify that you do not want environment strings expanded.

NoteNote

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), the GetValue method does not expand it. You can expand such a string after it has been retrieved by calling the ExpandEnvironmentVariables method.

NoteNote

The recommended way to retrieve data from the PerformanceData key is to use the PerformanceCounter class rather than the Microsoft.Win32.RegistryKey.GetValue method.

The following code example creates a test key with a value and retrieves that value. The example then attempts to retrieve a nonexistent value from the key; in this case the GetValue method returns the specified default value.

using System;
using Microsoft.Win32;

class RegGetDef {

    public static void Main() {

        // Create a reference to a valid key.  In order for this code to
        // work, the indicated key must have been created previously.
        // The key name is not case-sensitive.
        RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\myTestKey", true);

        // Get the value from the specified name/value pair in the key.
        String s = (String)rk.GetValue("testVal");

        Console.WriteLine(s);

        // Attempt to retrieve a value that does not exist; the specified
        // default value is returned.
        s = (String)rk.GetValue("notavalue", "The default to return");

        Console.WriteLine(s);
    }
}
/*
Output:
testData
The default to return
*/

import System.*;
import Microsoft.Win32.*;

class RegGetDef
{
    public static void main(String[] args)
    {
        // Create a reference to a valid key.  In order for this code to
        // work, the indicated key must have been created previously.
        // The key name is not case-sensitive.
        RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\myTestKey",
            true);
        // Get the value from the specified name/value pair in the key.
        String s = (String)(rk.GetValue("testVal"));

        Console.WriteLine(s);
        // Attempt to retrieve a value that does not exist; the specified
        // default value is returned.
        s = (String)(rk.GetValue("notavalue", "The default to return"));

        Console.WriteLine(s);
    } //main
} //RegGetDef

/*
Output:
testData
The default to return
*/

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft