Working with Registry Using .NET

Contents

  • Introduction
  • Basics of Windows Registry
  • Dealing with Registry using .NET
  • C#.NET and Registry
  • Creating SubKey
  • Reading and Writing Values
  • Deleting a subkey
  • Note
  • References

Introduction

The Windows Registry stores information for the operating system as well as applications in a system. It acts as a central repository. Developers always prefer to store the application values, such as file path, folder path, and so on, or configuration settings like connection strings in the Windows Registry.

Let us consider an example for a better understanding. If an application prompts the user to select a file path or folder path for the first time, selected values are stored in the Registry with an appropriate name. The next time the user launches the same application from the same machine, the previously selected values are fetched from the Registry and shown in the application, instead of selecting the same path again.

Going forward, this article explains some of the basics of the Windows Registry and proceeds with Registry operations. NET.

Basics of Windows Registry

The information stored in the Registry is accessible to other applications and users, so therefore secured information should not be stored in the Registry. The registry is organized in a hierarchical structure. It has predefined keys under which all data is stored and accessed. These keys cannot be renamed or deleted by the user. Let’s see a brief description of the predefined keys.

Predefined Key Description
HKEY_CURRENT_USER Contains the configuration information for the user currently logged onto the system, that is the user's profile data is stored here
HKEY_USERS Contains all user profiles on the computer. HKEY_CURRENT_USER is actually an alias for a key in the HKEY_USERS subtree.
HKEY_LOCAL_MACHINE Contains configuration information specific to the computer, irrespective of which user is logged on.
HKEY_CLASSES_ROOT Contains data that associates file types with programs, and configuration data for COM objects.
HKEY_CURRENT_CONFIG Contains information about the hardware profile used by the local computer at system startup.

Environment
Figure 1.1

To open the registry, go to the Run command and type regedit. Each and every predefined key has many keys and each key has one value. The preceding figure shows a snapshot of the registry.

Dealing with Registry using .NET

Communication with Windows Registry from .NET is done using the namespace system.Microsoft.Win32. The following Figure 1.2 represents the structure of the namespace.

Dealing with Registry
Figure 1.2

C#.NET and Registry

I have added a project to this document for the practical implementation of registry operations.

Creating SubKey

public void CreateRegistryKey()  
{  
    RegistryKey regkey;  
    regkey = Registry.CurrentUser.OpenSubKey("Software", true); //LINE : 1  
    regkey.CreateSubKey("REGISTRY_OPERATIONS"); //LINE : 2  
  
}  

In the preceding code, a registry subkey is created in the current user base key, with the hierarchy, HKEY_CURRENT_USER -> Software-> REGISTRY_OPERATIONS.

  • LINE 1: Open the sub-key named “Software”, the second boolean parameter denotes whether the key is writable or not. For example, you can set it to false, if you are reading from the Registry.
  • LINE 2: Creating a sub-key under a key.

Reading and Writing Values

RegistryKey regkey;
regkey.OpenSubKey("Software\\REGISTRY_OPERATIONS", true);
regkey.SetValue("FOLDER PATH", "C:\\Downloads"); // LINE: 1
string folderPath = regkey.GetValue("FOLDER PATH", "").ToString(); // LINE: 2
  • LINE1 will set the registry values in a specified path.
  • LINE2 will get the registry value for the specified key.

Deleting a subkey

RegistryKey regkey;
regkey.OpenSubKey("Software", true);
regkey.DeleteSubKey("REGISTRY_OPERATIONS", true);
regkey.Close();

In the above code snippet, I have deleted the subkey REGISTRY_OPERATIONS and all its key values.

Note. The Boolean value true is passed in the delete method, this is because an exception is thrown if the key to be deleted is not found.

Note

  • To read and write to the registry you need security permissions. If you do not have sufficient permissions, then you will get a SecurityException when you try to access or create keys.
  • The registry is a very sensitive part of the Windows operating system, so it is imperative that you take a backup of the registry before attempting to play around with it. A corrupt registry could render the operating system non-functional.