Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#
Article

How to Deal with Windows Registry?

Rate me:
Please Sign up or sign in to vote.
3.47/5 (21 votes)
27 Mar 2007CPOL5 min read 61.7K   364   23   21
Dealing with Registry to Save and Load My Application Settings and to modify Windows Functions

Introduction

In this article, we will talk about the Registry: What is it? How can we use it? How can developers use the Registry in their applications? How can we use the Registry in organizing our Windows user interface?

What is the Registry?

It is a central hierarchical database used in Microsoft Windows 9X, Windows CE, Windows NT, and Windows 2000 used to store information necessary to configure the system for one or more users, applications and hardware devices.

The Registry contains information that Windows continually references during operation, such as profiles for each user, the applications installed on the computer and the types of documents that each can create, property sheet settings for folders and application icons, what hardware exists on the system, and the ports that are being used.

How can we Modify the Registry?

You can modify the registry by using the registry editor (regedit.exe). Open "Start Menu", then choose "Run…" and type "regedit", then you will get the Registry Editor window on the screen. HOWEVER, if you use Registry Editor incorrectly, you can cause serious problems that may require you to reinstall your operating system.

What will you Get after Opening the Registry Editor?

You will get a tree view in the left-hand side of the editor which has 5 main nodes as follows:

  1. HKEY_CURRENT_USER (which we will use)

    This contains the root of the configuration information for the user who is currently logged on. The user's folders, screen colors, and Control Panel settings are stored here. This information is associated with the user's profile. This key is sometimes abbreviated as "HKCU."

  2. HKEY_USERS

    It contains all the actively loaded user profiles on the computer. HKEY_CURRENT_USER is a sub-key of HKEY_USERS. HKEY_USERS is sometimes abbreviated as "HKU." 

  3. HKEY_LOCAL_MACHINE

    It contains configuration information particular to the computer, (for any user). This key is sometimes abbreviated as "HKLM." 

  4. HKEY_CLASSES_ROOT

    Is a sub key of HKEY_LOCAL_MACHINE\software. The information stored here makes sure that the correct program opens when you open a file by using Windows Explorer. To change the settings for the interactive user, changes must be made under HKEY_CURRENT_USER\Software\Classes instead of under HKEY_CLASSES_ROOT. To change the default settings, changes must be made under HKEY_LOCAL_MACHINE\Software\Classes

  5. HKEY_CURRENT_CONFIG

    This contains information about the hardware profile that is used by the local computer at system startup.

How can you Use the Registry to Save and Load your Application Settings?

If you use any application like Microsoft Notepad and change the font color and size, then change the size of the Notepad, then drag the Notepad to the top-left corner of the screen and close it, what will you get when you open the Notepad the next time? You will get the Notepad with the same font, font size, size and place as the last time you left it. How can this happen? This is because of using the Registry to save the settings of the application when closing it and loading these settings when opening this application again.

To save your application settings, follow these steps while saving:

  1. You must include the namespace Microsoft.Win32.

  2. Create your own Registry key:

    C#
    RegistryKey MyKey = Registry.CurrentUser.CreateSubKey(@"Software\MyApp\");
  3. Set the values you want to add to Registry:

    C#
    MyKey.SetValue("Name","Value");
  4. Repeat step 3 for every value you want to add in the Registry.

  5. Don't forget to close your key

    C#
    MyKey.Close();
  6. Check that the key is created in the Registry. Open the Registry editor and choose the current user node, then the Software subnode and you will find your "MyApp" folder which contains the keys you set. Also, you can change the values you added by just right clicking on the key you want to change and clicking on 'modify'.

To load your application settings which are saved in the Registry, follow these steps:

  1. Don't forget:

    C#
    Using Microsoft.Win32;
  2. Create a Registry key:

    C#
    RegistryKey MyKey = Registry.CurrentUser.OpenSubKey(@"Software\MyApp\");
  3. To get your values from Registry:

    C#
    MyKey.GetValue("Name");
  4. Don't forget also to close your key:

    C#
    MyKey.Close(); 

How to Use the Registry to Control your Windows Settings?

You can also use Registry to control your Windows functions, including: Accessories, Appearance, Desktop, File System, LogIn and Authentication, Start Menu and TaskBar, Startup and Shutdown, System, Troubleshooting and finally Windows Explorer.

For example, you want to remove the log off and search from the start menu. How can you do that?

It is very simple. You just need to know the path to where the key is located, the name of the key and the value that you will exchange the current value with. The "search" key is located at "CurrentUser\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\", it's key is called "NoFind" and finally its value is either 1 or 0; 1 for true, (true means that no search button in start menu) and 0 for false, (the search button will be there). The logoff button is at the same path of the search button and its key name is "StartMenuLogoff" with values also 1 or 0, 1 for disabling the logoff button and 0 for enabling it.

This is a code sample for disabling the search and logoff buttons of the start menu:

C#
RegistryKey MyKey = Registry.CurrentUser.CreateSubKey
	(@"Software\ Microsoft\Windows\CurrentVersion\Policies\Explorer\");
MyKey.SetValue("NoFind",1); 
MyKey.SetValue("StartMenuLogoff",1);

Then restart (or logoff) your PC and see what happens. What if you want to get them back? Write the same code but replace the two values from 1 to 0 then restart again and check what happened.

If you are worried about memorizing the key's path, name and value, do not be, you can find all of them here.

I hope this article has been helpful to those of you who wondered what the registry really is, and I urge those of you who have other tid-bits to leave your comments. For those with further queries, please do not hesitate to ask.

History

  • 27th March, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Egypt Egypt
.NET Developer
Works now as a SoftwareDeveloper and Trainer

Academics:
BSc Computer and Information Sciences June 2006.

Certifications:
MCAD C#.Net

Comments and Discussions

 
Generalhi my problem is also related to parallel port registory Pin
rajbhansingh13-Jul-07 19:52
rajbhansingh13-Jul-07 19:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.