RegistryKey.GetValue Method (String, Object, RegistryValueOptions) (Microsoft.Win32)

Switch View :
ScriptFree
.NET Framework Class Library
RegistryKey.GetValue Method (String, Object, RegistryValueOptions)

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

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

Visual Basic
<ComVisibleAttribute(False)> _
Public Function GetValue ( _
	name As String, _
	defaultValue As Object, _
	options As RegistryValueOptions _
) As Object
C#
[ComVisibleAttribute(false)]
public Object GetValue(
	string name,
	Object defaultValue,
	RegistryValueOptions options
)
Visual C++
[ComVisibleAttribute(false)]
public:
Object^ GetValue(
	String^ name, 
	Object^ defaultValue, 
	RegistryValueOptions options
)
F#
[<ComVisibleAttribute(false)>]
member GetValue : 
        name:string * 
        defaultValue:Object * 
        options:RegistryValueOptions -> Object 

Parameters

name
Type: System.String
The name of the value to retrieve. This string is not case-sensitive.
defaultValue
Type: System.Object
The value to return if name does not exist.
options
Type: Microsoft.Win32.RegistryValueOptions
One of the enumeration values that specifies optional processing of the retrieved value.

Return Value

Type: System.Object
The value associated with name, processed according to the specified options, or defaultValue if name is not found.
Exceptions

Exception Condition
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.

ArgumentException

options is not a valid RegistryValueOptions value; for example, an invalid value is cast to RegistryValueOptions.

UnauthorizedAccessException

The user does not have the necessary registry rights.

Remarks

Use this overload to specify special processing of the retrieved value. For example, you can specify RegistryValueOptions.DoNotExpandEnvironmentNames when retrieving a registry value of type RegistryValueKind.ExpandString to retrieve the string without expanding embedded environment variables.

Use the defaultValue parameter to specify the value to return if name does not exist.

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

GetValue does not support reading values of type REG_NONE or REG_LINK. In both cases, the default value (null) is returned instead of the actual value.

Examples

The following code sample creates a test key, adds a value with an embedded environment variable, and retrieves the value in both expanded and unexpanded forms.

Visual Basic

Imports System
Imports Microsoft.Win32
Imports Microsoft.VisualBasic

Public Class Example
    Public Shared Sub Main()
        ' Delete and recreate the test key.
        Registry.CurrentUser.DeleteSubKey("RegistryValueOptionsExample", False)
        Dim rk As RegistryKey = _
            Registry.CurrentUser.CreateSubKey("RegistryValueOptionsExample")

        ' Add a value that contains an environment variable.
        rk.SetValue("ExpandValue", "The path is %PATH%", _
            RegistryValueKind.ExpandString)

        ' Retrieve the value, first without expanding the environment 
        ' variable and then expanding it.
        Console.WriteLine("Unexpanded: ""{0}""", _
            rk.GetValue("ExpandValue", "No Value", _
            RegistryValueOptions.DoNotExpandEnvironmentNames))
        Console.WriteLine("Expanded: ""{0}""", rk.GetValue("ExpandValue"))
    End Sub 'Main
End Class 'Example


C#

using System;
using Microsoft.Win32;
using Microsoft.VisualBasic;

public class Example
{
    public static void Main()
    {
        // Delete and recreate the test key.
        Registry.CurrentUser.DeleteSubKey("RegistryValueOptionsExample", false);
        RegistryKey rk = 
            Registry.CurrentUser.CreateSubKey("RegistryValueOptionsExample");

        // Add a value that contains an environment variable.
        rk.SetValue("ExpandValue", "The path is %PATH%", RegistryValueKind.ExpandString);

        // Retrieve the value, first without expanding the environment 
        // variable and then expanding it.
        Console.WriteLine("Unexpanded: \"{0}\"", 
            rk.GetValue("ExpandValue", "No Value", 
            RegistryValueOptions.DoNotExpandEnvironmentNames));
        Console.WriteLine("Expanded: \"{0}\"", rk.GetValue("ExpandValue"));
    } //Main
} //Example


Visual C++

#using <Microsoft.VisualBasic.dll>

using namespace System;
using namespace Microsoft::Win32;
using namespace Microsoft::VisualBasic;

int main()
{
    // Delete and recreate the test key.
    Registry::CurrentUser->DeleteSubKey( L"RegistryValueOptionsExample", false );
    RegistryKey ^ rk = 
        Registry::CurrentUser->CreateSubKey( L"RegistryValueOptionsExample" );

    // Add a value that contains an environment variable.
    rk->SetValue( L"ExpandValue", L"The path is %PATH%", 
        RegistryValueKind::ExpandString );

    // Retrieve the value, first without expanding the environment
    // variable and then expanding it.
    Console::WriteLine( L"Unexpanded: \"{0}\"", 
                        rk->GetValue( L"ExpandValue", 
                        L"No Value", 
                        RegistryValueOptions::DoNotExpandEnvironmentNames ) );
    Console::WriteLine( L"Expanded: \"{0}\"", rk->GetValue( L"ExpandValue" ) );

    return 0;
} //Main


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
.NET Framework Security

Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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

Reference