Read and Write Windows Registry to Store Data Using C#

The Windows Registry is a hierarchical database for storing many kinds of system and application settings. Prior to the Registry, .ini files in the form of text files were commonly used for storing these settings. However, these files were unable to meet all the requirements of a modern application. Especially in multi-user scenarios the .ini files were nearly useless.

On the other hand, the Windows Registry uses one logical repository that is able to store user-specific settings. According to Microsoft, there are several advantages over the obsolete .ini files like faster parsing, backup or restoration.

Structure of the Registry

The Registry is based on the two basic elements, keys and values and the entire structure is a tree with several root elements that slightly differ depending on the version of Windows you are currently using. The keys are container objects very similar to folders that can contain other keys or values. The values can be a string, binary, or DWORD depending on the scenario.

The most common root element structure is as follows:


Figure 1: root elements structure

HKEY_CLASSES_ROOT

This root element holds the information about registered (installed) applications and associated file extensions. For example, Windows is able to open the .pdf extension with Acrobat Reader because of the settings in this key. It is not advised to alter these keys manually and the Folder Options in the Windows Explorer should be used instead.

HKEY_CURRENT_USER

This root element represents the currently logged-in user and their specific settings. It is a link to a subkey of HKEY_USERS that corresponds to the current user. It cannot be edited.

HKEY_LOCAL_MACHINE

This root element contains five subkeys (hardware, security accounts manager, security, software, system) that are used for storing many kinds of settings used by the operating system. Hardware, security and security accounts manager subkeys can't be edited. It is not advised to manully alter the rest as it might result in a system crash.

HKEY_USERS

This root element holds all the user profiles used on the machine. Even though it can be edited, you should be very cautious when doing so.

HKEY_CURRENT_CONFIG

This root element contains read-only settings about the available hardware settings. These settings are not permanently stored on disk, but generated at the boot time and updated at runtime.

Windows Registry and C#

When it comes to using the Registry programmatically, we can easily store, retrieve and remove the settings in code using C#. Basically, the Registry and RegistryKey classes from the Microsoft.Win32 namespace are all we need to communicate with it.

Registry class

This class is a gate keeper and allows us to access the root elements. It provides RegistryKey objects of the root keys and several static methods to access key/value pairs.

RegistryKey class

An instance of this class represents a key-level node in the Registry.

Storing data in the Registry

In order to store any data in the Registry, we need to access a subkey of one of the predefined root elements. This is done by calling the static CreateSubKey method of the Registry class that will return the RegistryKey object we can manipulate it with.

Afterwards, we can store as many values in the subkey as we want by calling the SetValue method that takes the 2 arguments, name as a string and value as an object.

  1. //accessing the CurrentUser root element  
  2. //and adding "OurSettings" subkey to the "SOFTWARE" subkey  
  3. RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\OurSettings"); 

  4. //storing the values  
  5. key.SetValue("Setting1""This is our setting 1");  
  6. key.SetValue("Setting2""This is our setting 2");  
  7. key.Close(); 
 
 Figure 2: storing data in the Registry

Note: There is no need to check whether the subkey already exists or not. The CreateSubKey method creates it or opens the existing one.

Retrieving data from the Registry

To retrieve any data from the Registry, the static OpenSubKey method of the Registry class is used that returns a RegistryKey instance of the given subkey. If the object is not null (in case the given subkey doesn't exist), we can start retrieving the values by calling the GetValue method that takes just one argument, the key name.
  1. //opening the subkey  
  2. RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\OurSettings");  
  3.               
  4. //if it does exist, retrieve the stored values  
  5. if (key != null)  
  6. {  
  7.     Console.WriteLine(key.GetValue("Setting1"));  
  8.     Console.WriteLine(key.GetValue("Setting2"));  
  9.     key.Close();  

 
 Figure 3: console output of retrieved settings
 

Using Windows Registry in a real example

In the following example, we will use the Registry for storing some user-specific settings. More specifically, we will store the size of the application's window and retrieve it the next time the application starts. Because we use the CurrentUser subkey, we will allow every user to have his own window size based on his preferences (in this case the size of the window when the application was closed).

First, we need to store the settings. We will do so in the form's Closing event since it best suits our needs.

  1. private void WinRegForm_FormClosing(object sender, FormClosingEventArgs e)  
  2. {  
  3.             RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\WinRegistry");  
  4.             key.SetValue("Width"this.Width);  
  5.             key.SetValue("Height"this.Height);  
  6.             key.Close();  
  7.  } 
The next step is to retrieve the stored values when the application starts. For this purpose, we use the form's Load event.
  1. private void WinRegForm_Load(object sender, EventArgs e)  
  2. {  
  3.             RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\WinRegistry");  
  4.             if (key != null)  
  5.             {  
  6.                 int width = int.Parse(key.GetValue("Width").ToString());  
  7.                 int height = int.Parse(key.GetValue("Height").ToString());  
  8.                 this.Size = new Size(width, height);  
  9.             }  

And here is the result:
 Figure 4: form after first start
 
Figure 5: form after resize before closing
 
Figure 6: form after next start - the size was correctly saved and loaded 
 
Note: The entire source code is available for download.

As you can see, using the Windows Registry in C# is straightforward, thus it can be easily used in your own projects for whatever reason.

I hope you enjoyed the reading and comments are more than welcome.

Next Recommended Readings