How to Read and Write System Registry Keys using C# (VS.Net 2005)

Normally, you don't interact directly with the registry; instead, you use some Windows utilities (such as those found in the Control Panel) or INI, SYS, BAT files etc. These tools allow you to customize the registry without actually working directly with it. There are, however, features that can only be set by editing the registry directly.

 

The hives

 

To get a better understanding of the inner workings of the registry, let us see the structure of the Registry.

 

The Registry has a hierarchal structure, like the directories on your hard disk. Each branch (denoted by a folder icon in the Registry Editor, see below) is called a Key. Each key can contain other keys, as well as Values. Each value contains the actual information stored in the Registry. There are three types of values; String, Binary, and DWORD - the use of these depends upon the context. 


2222.gif

 

The registry structure is as follows:

 

HKEY_CLASSES_ROOT

This key contains file extension associations. For instance, Windows can recognize a .doc file as a Microsoft Word document because of the settings in this key. Use the Folder Options command from the Tools menu in Windows Explorer instead of adding new extensions to this key.

 

HKEY_CURRENT_USER

This key holds profile information for the user that is currently logged on. Each time a user logs on; the user's profile is copied from the HKEY_USERS key to the HKEY_CURRENT_USER key. This key cannot be edited

 

HKEY_LOCAL_MACHINE

This key holds hardware and software information within its five subkeys, which are the following:  

  • Hardware - The Hardware subkey stores settings for device drivers, IRQ hooks, and so forth. It is re-created each time your computer boots.

  • Security accounts manager (SAM) - The SAM subkey stores information on security settings, user accounts and group memberships.

  • Security - The Security subkey holds information on local security policies such as password policy, user rights, account lockout and so forth.

  • Software - The Software subkey, which applies to all local users, stores data about installed software.

  • System - The System subkey stores information needed to boot Windows.

The first three (Hardware, SAM, and Security) cannot be modified.

 

HKEY_USERS

This key contains the default profile as well as profiles for all users who have logged on to the computer. This key can be edited, but exercise care when doing so.

 

HKEY_CURRENT_CONFIG

This key holds hardware information that is currently in use and allows for backwards compatibility with older applications and device drivers. The information stored here cannot be edited.

 

In this article you will see that how can we save user preferences in the System Registry.

Registry entry comprises to primary elements: Name and Value. Each entry is also stored in a collection of keys and subkeys. Any parent key may have one or more child keys, each with one or more names and values.

 

Fig 1.1 shows the snapshot of Registry Editor highlighting the User Application (Application developed by you and installed on your PC) setting.

 

3333.gif

 

Fig 1.1: Snapshot of Registry Editor

 

In this article you will get the good knowledge to manipulate the Registry settings, but don't do any changes with the other applications registry setting because it might affect the Operating system and you have to do the installation again.

 

Before starting the coding ......Follow the instruction according to the figure

4444.gif

 

Declare:-

 

string strWindowsState;

string strPath;

 

Read from Registry:-

RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey(strPath); 

             strWindowsState = (string)regKeyAppRoot.GetValue("WindowState"); 

            if (strWindowsState != null && strWindowsState.CompareTo("Maximized") == 0)

                WindowState = System.Windows.Forms.FormWindowState.Maximized; 

            else if (strWindowsState != null && strWindowsState.CompareTo("Minimized") == 0)

                WindowState = System.Windows.Forms.FormWindowState.Minimized; 

            else

                WindowState = FormWindowState.Normal;

            label1.Text = strWindowsState;

            label3.Text = "";

            return;

 

Writing into Registry:-

 

strWindowsState = "";

            RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey(strPath); 

            if (WindowState == FormWindowState.Maximized)

                strWindowsState = "Maximized"; 

            else if(WindowState == FormWindowState.Maximized)

                strWindowsState = "Minimized"; 

            else

                 strWindowsState = "Normal"; 

             regKeyAppRoot.SetValue("WindowState", strWindowsState);

             label3.Text = strWindowsState;

             label1.Text = "";

             return;

 

How to test:

After running the application change the window state of the form to minimized then click on write button.....it will write into Registry.... Then click on read button..... you can see that the state value is changing.

 

Output

5555.gif

 

Note:-The Hardware, SAM, and Security cannot be modified.

Up Next
    Ebook Download
    View all
    Learn
    View all