Export (0) Print
Expand All

Registry Class

Supplies the base Registrykeys that access values and subkeys in the registry.

For a list of all members of this type, see Registry Members.

System.Object
   Microsoft.Win32.Registry

[Visual Basic]
NotInheritable Public Class Registry
[C#]
public sealed class Registry
[C++]
public __gc __sealed class Registry
[JScript]
public class Registry

Thread Safety

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

Remarks

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 once 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 for each user by storing the information in a different location in the registry.

The base (root) RegistryKey instances that are exposed by Registry delineate the basic storage mechanism for subkeys and values in the registry. The keys are all readonly since the registry depends on their existence. The keys exposed by Registry are:

CurrentUser
Stores information about user preferences.
LocalMachine
Stores configuration information for the local machine.
ClassesRoot
Stores information about types (and classes) and their properties.
Users
Stores information about the default user configuration.
PerformanceData
Stores performance information for software components.
CurrentConfig
Stores non-user-specific hardware information.
DynData
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.

Example

[Visual Basic, C#, C++] The following example demonstrates how to retrieve the subkeys of this key, and prints 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.

[Visual Basic] 
Imports System
Imports Microsoft.Win32

Class Reg
    
    Public Shared Sub Main()
        
        ' Create a RegistryKey, which will access the HKEY_USERS
        ' key in the registry of this machine.
        Dim rk As RegistryKey = Registry.Users
        
        ' Print out the keys.
        PrintKeys(rk)
    End Sub    
    
    Shared Sub PrintKeys(rkey As RegistryKey)
        
        ' Retrieve all the subkeys for the specified key.
        Dim names As String() = rkey.GetSubKeyNames()
        
        Dim icount As Integer = 0
        
        Console.WriteLine("Subkeys of " & rkey.Name)
        Console.WriteLine("-----------------------------------------------")
        
        ' Print the contents of the array to the console.
        Dim s As String
        For Each s In  names
            Console.WriteLine(s)
            
            ' The following code puts a limit on the number
            ' of keys displayed.  Comment it out to print the
            ' complete list.
            icount += 1            
            If icount >= 10 Then
                Exit For
            End If
        Next s
    End Sub
End Class

[C#] 
using System;
using Microsoft.Win32;

class Reg {
    public static void 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);
    }

    static void PrintKeys(RegistryKey rkey) {

        // Retrieve all the subkeys for the specified key.
        String [] names = rkey.GetSubKeyNames();

        int icount = 0;

        Console.WriteLine("Subkeys of " + rkey.Name);
        Console.WriteLine("-----------------------------------------------");

        // Print the contents of the array to the console.
        foreach (String s in names) {
            Console.WriteLine(s);

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

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace Microsoft::Win32;

void PrintKeys(RegistryKey* rkey) {

    // Retrieve all the subkeys for the specified key.
    String* names[] = rkey->GetSubKeyNames();

    int icount = 0;

    Console::WriteLine(S"Subkeys of {0}", rkey->Name);
    Console::WriteLine(S"-----------------------------------------------");

    // Print the contents of the array to the console.
    System::Collections::IEnumerator* enum0 = names->GetEnumerator();
    while (enum0->MoveNext())
    {
        String* s = __try_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.
        icount++;
        if (icount >= 10)
            break;
    }
}

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);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: Microsoft.Win32

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

Registry Members | Microsoft.Win32 Namespace | RegistryHive | RegistryKey

Show:
© 2014 Microsoft