1 Usage Of RegistryKey Class 6th October 2009, 8:32 pm
sHa92
Founder
Usage Of RegistryKey Class by Shabbir
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
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
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.
[You must be registered and logged in to see this image.]
You will see the Dispose function there, so in that function above this code
write the following code
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 Project Here
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
- Code:
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
- Code:
RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true).
CreateSubKey("Registry Test");
if (key.GetValue("FirstStart") == null)
MessageBox.Show("The application is started for the first time.\n"+
"I will not load the settings.", "Info");
else
{
this.Left = (int)key.GetValue("Left");
this.Top = (int)key.GetValue("Top");
this.Height = (int)key.GetValue("Height");
this.Width = (int)key.GetValue("Width");
}
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.
[You must be registered and logged in to see this image.]
You will see the Dispose function there, so in that function above this code
- Code:
base.Dispose(disposing);
write the following code
- Code:
RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true).
CreateSubKey("Registry Test");
key.SetValue("FirstStart", 0, RegistryValueKind.DWord);
key.SetValue("Left", this.Left, RegistryValueKind.DWord);
key.SetValue("Top", this.Top, RegistryValueKind.DWord);
key.SetValue("Height", this.Height, RegistryValueKind.DWord);
key.SetValue("Width", this.Width, RegistryValueKind.DWord);
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 Project Here
- Code:
http://www.codeitwell.com/wp-content/downloads/UseOfRegistry.rar