C# Registry Operations

When working with the registry in C#, you have the option to access it using either the functions provided by C# or the registry classes available in the .NET Framework. The .NET Framework offers two key classes within the Microsoft.Win32 namespace for registry operations: the Registry class and the RegistryKey class.

The Registry class provides access to the base registry keys through shared public methods. These methods allow you to perform common operations on the registry without explicitly opening or closing registry keys. Here are some of the important shared methods provided by the Registry class:

  1. HKEY_CURRENT_USER
  2. HKEY_USERS
  3. HKEY_LOCAL_MACHINE
  4. HKEY_CLASSES_ROOT
  5. HKEY_CURRENT_CONFIG

Each key has many subkeys and may have a value.

Once you have access to a base registry key using the Registry class, you can further manipulate the registry by working with instances of the RegistryKey class. The RegistryKey class provides methods and properties to perform operations like creating, deleting, reading, and writing registry keys and values. The Registry class provides base registry keys as shared public methods:

using Microsoft.Win32;

Creating a Registry Entry

rKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true); rKey.CreateSubKey("AppReg");

Above code shows how to create a subkey under HKLM\Software called AppReg.

Deleting a Subkey

rKey = Registry.LocalMachine.OpenSubKey("Software", true); rKey.DeleteSubKey("AppReg", true);

The following C# program shows how to create a registry entry , set values to registry entries, retrieve values from registry and delete keys on Registry. Drag and drop four buttons on the form control and copy and paste the following source code.

Full Source C#
using System; using System.Data; using System.Windows.Forms; using Microsoft.Win32; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { RegistryKey rKey ; rKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true); rKey.CreateSubKey("AppReg"); rKey.Close(); MessageBox.Show ("AppReg created !"); } private void button2_Click(object sender, EventArgs e) { RegistryKey rKey ; rKey = Registry.LocalMachine.OpenSubKey("Software\\AppReg", true); rKey.SetValue("AppName", "RegApp"); rKey.SetValue("Version", 1.1); rKey.Close(); MessageBox.Show("Appname , version created"); } private void button3_Click(object sender, EventArgs e) { RegistryKey rKey ; Object ver ; string app = null; rKey = Registry.LocalMachine.OpenSubKey("Software\\AppReg", true); app = (string)rKey.GetValue("AppName"); ver = rKey.GetValue("Version"); rKey.Close(); MessageBox.Show("App Name " + app + " Version " + ver.ToString()); } private void button4_Click(object sender, EventArgs e) { RegistryKey rKey ; rKey = Registry.LocalMachine.OpenSubKey("Software", true); rKey.DeleteSubKey("AppReg", true); rKey.Close(); MessageBox.Show("AppReg deleted"); } } }

Conclusion

Using the Registry class and the RegistryKey class from the Microsoft.Win32 namespace, you can access and manipulate registry settings in your C# applications. This allows you to store and retrieve application-specific and user-specific settings efficiently and reliably.