Click here to Skip to main content
Click here to Skip to main content

How to Deal with Windows Registry?

By , 27 Mar 2007
Rate this:
Please Sign up or sign in to vote.

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:

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

    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

    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:

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

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

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

    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:

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)

About the Author

Moustafa Zain
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

 
Generalfantstic article PinmemberMember 435004212-Jun-08 20:45 
GeneralRe: fantstic article PinmemberMoustafa-Mohammed28-Jun-08 4:10 
QuestionRegistry or File PinmemberNigelAston17-Jul-07 23:03 
AnswerRe: Registry or File PinmemberMoustafa-Mohammed23-Jul-07 2:27 
Generalhi my problem is also related to parallel port registory Pinmemberrajbhansingh13-Jul-07 19:52 
QuestionHi PinmemberNilish23-May-07 19:46 
can i hide the run ?
please provide the path.
AnswerRe: Hi [modified] PinmemberMoustafa-Mohammed28-May-07 6:03 
Generalgood job PinmemberIslam931-Mar-07 16:06 
GeneralRe: good job PinmemberMoustafa-Mohammed1-Apr-07 8:27 
Generalgood job mostafa Pinmembermetinergoktas30-Mar-07 4:00 
GeneralRe: good job mostafa PinmemberMoustafa-Mohammed30-Mar-07 6:28 
Questionhow to protect PinmemberBharaathi29-Mar-07 21:18 
AnswerRe: how to protect PinmemberMoustafa-Mohammed30-Mar-07 6:29 
AnswerRe: how to protect PinmemberAmr Aly Elsehemy31-Mar-07 6:37 
AnswerRe: how to protect PinmemberMoustafa-Mohammed31-Mar-07 7:55 
GeneralNice work Mustafa Pinmemberamjad mohammad29-Mar-07 4:37 
GeneralRe: Nice work Mustafa PinmemberMoustafa-Mohammed29-Mar-07 5:11 
GeneralGood Start PinmemberRaghda Amr Salah Eldin28-Mar-07 10:29 
GeneralRe: Good Start PinmemberMoustafa-Mohammed29-Mar-07 3:04 
GeneralNice demo PinmemberAmr Aly Elsehemy28-Mar-07 4:45 
GeneralRe: Nice demo PinmemberMoustafa-Mohammed28-Mar-07 9:04 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 27 Mar 2007
Article Copyright 2007 by Moustafa Zain
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid