Wednesday 22 August 2018

C#: Working with Registries

Windows Registry is a hierarchical kind of database comes with windows operating system, used to store system, application, user specific details.

How to open Windows Registry?
Open run prompt (Windows + r), type ‘regedit’ and press Enter. You can able to see following structure.




HKEY_CLASSES_ROOT
This root element holds information about installed applications and their associated file extensions. For example, VLC, Windows media player applications register their supported file formats.

HKEY_CURRENT_USER
The entries under this root element holds currently logged-in user and their specific settings.

HKEY_LOCAL_MACHINE




This root element contains 5 subkeys.
a.   HARDWARE
b.   SAM (Security Accounts Manager)
c.   SECURITY
d.   SOFTWARE
e.   SYSTEM

These keys store settings that are used by operating system. It is adviced to not alter these information.

HKEY_USERS
This root element holds all the user profiles used in this machine.

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.

Working with Registry using C#
C# provides two basic classes ‘Registry’, ‘RegistryKey’ to work with registry entries.

Registry class
It is used to access root elements of Registry Editor. By using this, you can access the RegistryKey objects of the root keys.

RegistryKey class
A Registrykey instance represents key-level node in the Registry.

Get all the keys of Registry root element
Registry class provide following static fields to access the root elements.

Field
Description
public static readonly RegistryKey ClassesRoot;
This field reads the Windows registry base key HKEY_CLASSES_ROOT.
public static readonly RegistryKey CurrentConfig;
This field reads the Windows registry base key HKEY_CURRENT_CONFIG.
public static readonly RegistryKey CurrentUser;
This field reads the Windows registry base key HKEY_CURRENT_USER
public static readonly RegistryKey LocalMachine;
This field reads the Windows registry base key HKEY_LOCAL_MACHINE. 
public static readonly RegistryKey PerformanceData;
This field reads the Windows registry base key HKEY_PERFORMANCE_DATA.
public static readonly RegistryKey Users;
This field reads the Windows registry base key HKEY_USERS.

Following program prints all the sub keys of the root element ‘HKEY_CURRENT_USER’

Program.cs

using System;
using Microsoft.Win32;

class Program
{
    static void Main()
    {
        RegistryKey currentUser = Registry.CurrentUser;
        PrintKeys(currentUser);
    }

    static void PrintKeys(RegistryKey rkey)
    {
        // Retrieve all the subkeys for the specified key.
        String[] names = rkey.GetSubKeyNames();

        Console.WriteLine("Subkeys of " + rkey.Name);
        Console.WriteLine("-----------------------------------------------");
        foreach (String s in names)
        {
            Console.WriteLine(s);
        }
    }
}

Output

Subkeys of HKEY_CURRENT_USER
-----------------------------------------------
AppEvents
AppXBackupContentType
Console
Control Panel
Environment
EUDC
Identities
Keyboard Layout
MDocsUser
Network
Printers
SOFTWARE
System
Volatile Environment

Storing data in a registry
By calling the 'CreateSubKey' method, you can create a sub key.

Following statements are used to store data.

                  RegistryKey myAppSettings = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\MyAppSettings");
       
        //storing the values 
        myAppSettings.SetValue("appName", "C# Tutorials");
        myAppSettings.SetValue("Demo", "Registry keys storage");

        myAppSettings.Close();


Program.cs

using System;
using Microsoft.Win32;

class Program
{
    static void Main()
    {
        RegistryKey myAppSettings = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\MyAppSettings");
        
        //storing the values  
        myAppSettings.SetValue("appName", "Tutorials");
        myAppSettings.SetValue("Demo", "Registry keys storage");

        PrintKeys(myAppSettings);

        myAppSettings.Close();
    }

    static void PrintKeys(RegistryKey rkey)
    {
        // Retrieve all the subkeys for the specified key.
        String[] names = rkey.GetSubKeyNames();

        Console.WriteLine("Subkeys of " + rkey.Name);
        Console.WriteLine("-----------------------------------------------");
        foreach (String s in names)
        {
            Console.WriteLine(s);
        }
    }
}

Read data from Registry
By using 'OpenSubKey' method, you can get RegistryKey instance.


Program.cs

using System;
using Microsoft.Win32;

class Program
{
    static void Main()
    {
        RegistryKey adobeSettings = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Adobe\Acrobat Reader\DC\AcroApp");
     
        PrintKeys(adobeSettings);
    }

    static void PrintKeys(RegistryKey rkey)
    {
        // Retrieve all the subkeys for the specified key.
        String[] names = rkey.GetSubKeyNames();

        Console.WriteLine("Subkeys of " + rkey.Name);
        Console.WriteLine("-----------------------------------------------");
        foreach (String key in names)
        {
            Console.WriteLine("{0} = {1}", key, rkey.GetValue(key));
        }
    }
}

Previous                                                 Next                                                 Home

No comments:

Post a Comment