This documentation is archived and is not being maintained.

Registry Class

Provides RegistryKey objects that represent the root keys in the Windows registry, and static methods to access key/value pairs.


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

public ref class Registry abstract sealed

The Registry type exposes the following members.

Public methodStatic memberGetValueRetrieves 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 nullptr if the specified key does not exist.
Public methodStatic memberSetValue(String, String, Object)Sets the specified name/value pair on the specified registry key. If the specified key does not exist, it is created.
Public methodStatic memberSetValue(String, String, Object, RegistryValueKind)Sets the name/value pair on the specified registry key, using the specified registry data type. If the specified key does not exist, it is created.

Public fieldStatic memberClassesRootDefines the types (or classes) of documents and the properties associated with those types. This field reads the Windows registry base key HKEY_CLASSES_ROOT.
Public fieldStatic memberCurrentConfigContains configuration information pertaining to the hardware that is not specific to the user. This field reads the Windows registry base key HKEY_CURRENT_CONFIG.
Public fieldStatic memberCurrentUserContains information about the current user preferences. This field reads the Windows registry base key HKEY_CURRENT_USER
Public fieldStatic memberDynData Obsolete. Contains dynamic registry data. This field reads the Windows registry base key HKEY_DYN_DATA.
Public fieldStatic memberLocalMachineContains the configuration data for the local machine. This field reads the Windows registry base key HKEY_LOCAL_MACHINE.
Public fieldStatic memberPerformanceDataContains performance information for software components. This field reads the Windows registry base key HKEY_PERFORMANCE_DATA.
Public fieldStatic memberUsersContains information about the default user configuration. This field reads the Windows registry base key HKEY_USERS.

This class provides the set of standard root keys found in the registry on machines running Windows. The registry is a storage facility for information about applications, users, and default system settings. For example, applications can use the registry for storing information that needs to be preserved after the application is closed, and access that same information when the application is reloaded. For instance, you can store color preferences, screen locations, or the size of the window. You can control this data for each user by storing the information in a different location in the registry.

The base, or root RegistryKey instances that are exposed by the Registry class delineate the basic storage mechanism for subkeys and values in the registry. All keys are read-only because the registry depends on their existence. The keys exposed by Registry are:


Stores information about user preferences.


Stores configuration information for the local machine.


Stores information about types (and classes) and their properties.


Stores information about the default user configuration.


Stores performance information for software components.


Stores non-user-specific hardware information.


Stores dynamic data.

Once you have identified the root key under which you want to store/retrieve information from the registry, you can use the RegistryKey class to add or remove subkeys, and manipulate the values for a given key.

Hardware devices can place information in the registry automatically using the Plug and Play interface. Software for installing device drivers can place information in the registry by writing to standard APIs.

Static Methods for Getting and Setting Values

In the .NET Framework version 2.0, the Registry class also contains static GetValue and SetValue methods for setting and retrieving values from registry keys. These methods open and close registry keys each time they are used, so they do not perform as well as analogous methods in the RegistryKey class, when you access a large number of values.

The RegistryKey class also provides methods that allow you to set Windows access control security for registry keys, to test the data type of a value before retrieving it, and to delete keys.

This section contains two code examples. The first example demonstrates root keys, and the second example demonstrates the static GetValue and SetValue methods.

Example 1

The following code example demonstrates how to retrieve the subkeys of the HKEY_USERS key, and print their names to the screen. Use the OpenSubKey method to create an instance of the particular subkey of interest. You can then use other operations in RegistryKey to manipulate that key.

using namespace System;
using namespace Microsoft::Win32;
void PrintKeys( RegistryKey ^ rkey )

   // Retrieve all the subkeys for the specified key.
   array<String^>^names = rkey->GetSubKeyNames();
   int icount = 0;
   Console::WriteLine( "Subkeys of {0}", rkey->Name );
   Console::WriteLine( "-----------------------------------------------" );

   // Print the contents of the array to the console.
   System::Collections::IEnumerator^ enum0 = names->GetEnumerator();
   while ( enum0->MoveNext() )
      String^ s = safe_cast<String^>(enum0->Current);
      Console::WriteLine( s );

      // The following code puts a limit on the number
      // of keys displayed.  Comment it out to print the
      // complete list.
      if ( icount >= 10 )

int main()

   // Create a RegistryKey, which will access the HKEY_USERS
   // key in the registry of this machine.
   RegistryKey ^ rk = Registry::Users;

   // Print out the keys.
   PrintKeys( rk );

Example 2

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.

using namespace System;
using namespace Microsoft::Win32;

int main()
    // The name of the key must include a valid root.
    String^ userRoot = "HKEY_CURRENT_USER";
    String^ subKey = "RegistrySetValueExample2";
    String^ keyName = String::Concat(userRoot, "\\", subKey);

    // An int value can be stored without specifying the
    // registry data type, but Int64 values will be stored
    // as strings unless you specify the type. Note that
    // the int is stored in the default name/value
    // pair.
    Registry::SetValue(keyName, "", 5280);
    Registry::SetValue(keyName, "TestInt64", 12345678901234, 

    // Strings with expandable environment variables are
    // stored as ordinary strings unless you specify the
    // data type.
    Registry::SetValue(keyName, "TestExpand", "My path: %path%");
    Registry::SetValue(keyName, "TestExpand2", "My path: %path%", 

    // Arrays of strings are stored automatically as 
    // MultiString. Similarly, arrays of Byte are stored
    // automatically as Binary.
    array<String^>^ strings  = {"One", "Two", "Three"};
    Registry::SetValue(keyName, "TestArray", strings);

    // Your default value is returned if the name/value pair
    // does not exist.
    String^ noSuch = (String^)Registry::GetValue(keyName, 
        "Return this default if NoSuchName does not exist.");
    Console::WriteLine("\r\nNoSuchName: {0}", noSuch);

    // Retrieve the int and Int64 values, specifying 
    // numeric default values in case the name/value pairs
    // do not exist. The int value is retrieved from the
    // default (nameless) name/value pair for the key.
    int testInteger = (int)Registry::GetValue(keyName, "", -1);
    Console::WriteLine("(Default): {0}", testInteger);
    long long testInt64 = (long long)Registry::GetValue(keyName, 
        "TestInt64", System::Int64::MinValue);
    Console::WriteLine("TestInt64: {0}", testInt64);

    // When retrieving a MultiString value, you can specify
    // an array for the default return value. 
    array<String^>^ testArray = (array<String^>^)Registry::GetValue(
        keyName, "TestArray", 
        gcnew array<String^> {"Default if TestArray does not exist."});
    for (int i = 0; i < testArray->Length; i++)
        Console::WriteLine("TestArray({0}): {1}", i, testArray[i]);

    // A string with embedded environment variables is not
    // expanded if it was stored as an ordinary string.
    String^ testExpand = (String^)Registry::GetValue(keyName, 
        "TestExpand", "Default if TestExpand does not exist.");
    Console::WriteLine("TestExpand: {0}", testExpand);

    // A string stored as ExpandString is expanded.
    String^ testExpand2 = (String^)Registry::GetValue(keyName, 
        "TestExpand2", "Default if TestExpand2 does not exist.");
        "TestExpand2: {0}...", testExpand2->Substring(0, 40));
        "\r\nUse the registry editor to examine the key.");
    Console::WriteLine("Press the Enter key to delete the key.");
// This code example produces output similar to the following:
// NoSuchName: Return this default if NoSuchName does not exist.
// (Default): 5280
// TestInt64: 12345678901234
// TestArray(0): One
// TestArray(1): Two
// TestArray(2): Three
// TestExpand: My path: %path%
// TestExpand2: My path: D:\Program Files\Microsoft.NET\...
// Use the registry editor to examine the key.
// Press the Enter key to delete the key.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.