This documentation is archived and is not being maintained.

RegistryKey.GetValue Method (String)

Retrieves the value associated with the specified name. Returns a null reference (Nothing in Visual Basic) if the name/value pair does not exist in the registry.

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

public Object GetValue (
	string name
)
public Object GetValue (
	String name
)
public function GetValue (
	name : String
) : Object
Not applicable.

Parameters

name

The name of the value to retrieve.

Return Value

The value associated with name, or a null reference (Nothing in Visual Basic) 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.

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(String,Object,RegistryValueOptions) method 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), GetValue 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.

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

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.

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.***]

*/

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

public class Example
{
    public static void main(String[] args)
    {
        // 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", (Int32)42, RegistryValueKind.QWord);
        // The following SetValue calls have the same effect as using the
        // SetValue overload that does not specify RegistryValueKind.
        //
        rk.SetValue("DWordValue", (Int32)42, RegistryValueKind.DWord);
        rk.SetValue("MultipleStringValue", new String[] { "One", "Two",
            "Three" }, RegistryValueKind.MultiString);
        rk.SetValue("BinaryValue", new ubyte[] { 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();

        for (int iCtr1 = 0; iCtr1 < valueNames.get_Length(); iCtr1++) {
            String s = valueNames [iCtr1];
            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.get_Length(); i++) {
                        if (i != 0) Console.Write(",");
                        Console.Write(" \"{0}\"", values.get_Item(i));
                    }
                    Console.WriteLine();
                    break;

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

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

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.***]

*/

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Show: