RegistryKey.GetValue Method

Definition

Retrieves the value associated with the specified name.

Overloads

GetValue(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.

GetValue(String)

Retrieves the value associated with the specified name. Returns null if the name/value pair does not exist in the registry.

GetValue(String, Object)

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

GetValue(String, Object, RegistryValueOptions)

Source:
RegistryKey.cs

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

C#
public object? GetValue(string? name, object? defaultValue, Microsoft.Win32.RegistryValueOptions options);
C#
public object GetValue(string name, object defaultValue, Microsoft.Win32.RegistryValueOptions options);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public object GetValue(string name, object defaultValue, Microsoft.Win32.RegistryValueOptions options);

Parameters

name
String

The name of the value to retrieve. This string is not case-sensitive.

defaultValue
Object

The value to return if name does not exist.

options
RegistryValueOptions

One of the enumeration values that specifies optional processing of the retrieved value.

Returns

The value associated with name, processed according to the specified options, or defaultValue if name is not found.

Attributes

Exceptions

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

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

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

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

The user does not have the necessary registry rights.

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.

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

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

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.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

GetValue(String)

Source:
RegistryKey.cs

Retrieves the value associated with the specified name. Returns null if the name/value pair does not exist in the registry.

C#
public object? GetValue(string? name);
C#
public object GetValue(string name);

Parameters

name
String

The name of the value to retrieve. This string is not case-sensitive.

Returns

The value associated with name, or null if name is not found.

Exceptions

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

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

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

The user does not have the necessary registry rights.

Examples

The following code example creates a test key and adds values of different data types to the key. The example then reads the name/value pairs and displays them to the console, using the GetValueKind method to retrieve the corresponding registry data types.

C#
using System;
using Microsoft.Win32;

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

        // Create name/value pairs.

        // This overload supports QWord (long) values.
        rk.SetValue("QuadWordValue", 42, RegistryValueKind.QWord);

        // The following SetValue calls have the same effect as using the
        // SetValue overload that does not specify RegistryValueKind.
        //
        rk.SetValue("DWordValue", 42, RegistryValueKind.DWord);
        rk.SetValue("MultipleStringValue", new string[] {"One", "Two", "Three"}, RegistryValueKind.MultiString);
        rk.SetValue("BinaryValue", new byte[] {10, 43, 44, 45, 14, 255}, RegistryValueKind.Binary);
        rk.SetValue("StringValue", "The path is %PATH%", RegistryValueKind.String);

        // This overload supports setting expandable string values. Compare
        // the output from this value with the previous string value.
        rk.SetValue("ExpandedStringValue", "The path is %PATH%", RegistryValueKind.ExpandString);

        // Display all name/value pairs stored in the test key, with each
        // registry data type in parentheses.
        //
        string[] valueNames = rk.GetValueNames();
        foreach (string s in valueNames)
        {
            RegistryValueKind rvk = rk.GetValueKind(s);
            switch (rvk)
            {
                case RegistryValueKind.MultiString :
                    string[] values = (string[]) rk.GetValue(s);
                    Console.Write("\r\n {0} ({1}) =", s, rvk);
                    for (int i = 0; i < values.Length; i++)
                    {
                        if (i != 0) Console.Write(",");
                        Console.Write(" \"{0}\"", values[i]);
                    }
                    Console.WriteLine();
                    break;

                case RegistryValueKind.Binary :
                    byte[] bytes = (byte[]) rk.GetValue(s);
                    Console.Write("\r\n {0} ({1}) =", s, rvk);
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        // Display each byte as two hexadecimal digits.
                        Console.Write(" {0:X2}", bytes[i]);
                    }
                    Console.WriteLine();
                    break;

                default :
                    Console.WriteLine("\r\n {0} ({1}) = {2}", s, rvk, rk.GetValue(s));
                    break;
            }
        }
    }
}
/*

This code example produces the following output:
 QuadWordValue (QWord) = 42

 DWordValue (DWord) = 42

 MultipleStringValue (MultiString) =, "One", "Two", "Three"

 BinaryValue (Binary) = 0A 2B 2C 2D 0E FF

 StringValue (String) = The path is %PATH%

 ExpandedStringValue (ExpandString) = The path is C:\Program Files\Microsoft.NET\SDK\v2.0\Bin;
 [***The remainder of this output is omitted.***]

*/

Remarks

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.

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(String, Object, RegistryValueOptions) method overload to specify that you do not want environment strings expanded.

Note

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 ExpandEnvironmentVariables method.

Note

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

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.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5

GetValue(String, Object)

Source:
RegistryKey.cs

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

C#
public object? GetValue(string? name, object? defaultValue);
C#
public object GetValue(string name, object defaultValue);

Parameters

name
String

The name of the value to retrieve. This string is not case-sensitive.

defaultValue
Object

The value to return if name does not exist.

Returns

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

Exceptions

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

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

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

The user does not have the necessary registry rights.

Examples

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.

C#
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", false);
        // Get the value from the specified name/value pair in the key.

        string valueName = "myTestValue";

        Console.WriteLine("Retrieving registry value ...");
        Console.WriteLine();
        object o = rk.GetValue(valueName);
        Console.WriteLine("Object Type = " + o.GetType().FullName);
        Console.WriteLine();
        switch (rk.GetValueKind(valueName))
        {
            case RegistryValueKind.String:
            case RegistryValueKind.ExpandString:
                Console.WriteLine("Value = " + o);
                break;
            case RegistryValueKind.Binary:
                foreach (byte b in (byte[])o)
                {
                    Console.Write("{0:x2} ", b);
                }
                Console.WriteLine();
                break;
            case RegistryValueKind.DWord:
                Console.WriteLine("Value = " + Convert.ToString((int)o));
                break;
            case RegistryValueKind.QWord:
                Console.WriteLine("Value = " + Convert.ToString((Int64)o));
                break;
            case RegistryValueKind.MultiString:
                foreach (string s in (string[])o)
                {
                    Console.Write("[{0:s}], ", s);
                }
                Console.WriteLine();
                break;
            default:
                Console.WriteLine("Value = (Unknown)");
                break;
        }

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

        rk.Close();
    }
}
/*
Output:
Retrieving registry value ...

Object Type = System.String

Value = testData

The default to return
*/

Remarks

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.

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.

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.

Note

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.

Note

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

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.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5