Usage Of RegistryKey Class

by Shabbir on May 8, 2008

in C#

The usage of RegistryKey class is very huge in many applications in which is needed to "remember" some settings of the application, and then load them when starting the application.
Today we will use the RegistryKey class for that issue. The application will save this informations in the registry when exiting:

  • Left
  • Top
  • Height
  • Width
  • FirstStart

And on start the application will load them.

Don't forget do add this code everywhere where you are using the RegistryKey class (see more here)

  1. using Microsoft.Win32;

So, create a C# Windows Forms Application, and make a double click on the form it will create the Form1_Load event, it is executed when application starts.

Then, in the Form1_Load event paste this code

  1. RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true).
  2. CreateSubKey("Registry Test");
  3. if (key.GetValue("FirstStart") == null)
  4. MessageBox.Show("The application is started for the first time.\n"+
  5. "I will not load the settings.", "Info");
  6. else
  7. {
  8. this.Left = (int)key.GetValue("Left");
  9. this.Top = (int)key.GetValue("Top");
  10. this.Height = (int)key.GetValue("Height");
  11. this.Width = (int)key.GetValue("Width");
  12. }
  13. key.Close();

With the above code we create an object from the RegistryKey class and create a sub key "Registry Test" in the HKEY_CURRENT_USER/Software key. Then we check if the application is started for the first time. If the "FirstStart" value does not exist then the application is started for the first time, and that means that the other values ("Left", "Top", "Height", "Width") are not saved, and when the program tries to load them it will fall.
So what we do, if the application is started for the first time, we do not load them, and popup a message to tell the user that. When loading the setting the value is casted to integer and then assigned to the appositely property. And then we close the key object.

Now go to solution explorer and open the Form1.Designer.cs file.

Form Designer class=

You will see the Dispose function there, so in that function above this code

  1. base.Dispose(disposing);

write the following code

  1. RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true).
  2. CreateSubKey("Registry Test");
  3. key.SetValue("FirstStart", 0, RegistryValueKind.DWord);
  4. key.SetValue("Left", this.Left, RegistryValueKind.DWord);
  5. key.SetValue("Top", this.Top, RegistryValueKind.DWord);
  6. key.SetValue("Height", this.Height, RegistryValueKind.DWord);
  7. key.SetValue("Width", this.Width, RegistryValueKind.DWord);
  8. key.Close();

This piece of code is called when we close the application, it saves the current values of the properties (Left, Top, Height, Width) and to value FirstStart assigns 0.

Build the application and start it. For the first time, it will popup a message box. Then move the application somewhere on your desktop, change its size and then close it, and the values will be recorded in the registry. The next time you start the application it will be on the same place you exited and its size will be the same.

Download the project

Sharing is Caring...

{ 2 comments… read them below or add one }

Michael May 10, 2008 at 6:48 pm

you got a nice blog ,
have you read the book
Registery of Windows XP by jerry honeycut ?
keep it up

Reply

admin May 10, 2008 at 8:12 pm

Thanks.
I have not read that book, I will keep on mind that book and will try to read when I will more free time.

Reply

Leave a Comment

Previous post:

Next post:

    About the Author

  • author photo

    My Name is Shabbir Bhimani and I am developer by profession in the field of applications, web and database. Currently doing full time online marketing and like to share my experiences on how you can make money online @ CodeItWell.com. Read more ...


    See how you can get in touch with me.