“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.
C# Programming Series
C# Accessing the Registry
Yashavant Kanetkar
Registry and RegistryKey Classes
The registry is arranged in a hierarchical tree like structure. Each node in this tree is called a key. Each key can contain additional keys or sub-keys (which allow further branching) as well as values that are used to store the actual data. Each registry key may have several values. For example a registry key 'Desktop' may have values like 'Wallpaper', 'TileWallpaper', etc. Each value in a key contains the actual data. This data may take several forms ranging from a simple integer value to a user-defined binary object. Each type is represented by a special registry specific type. For example an integer is represented by REG_DWORD and a string is represented by REG_SZ.
Many applications store their status viz. last file opened in the application, options selected by the user, etc. in registry. We would also create such an application that would store the background image, foreground color, size of the window and its position in the registry so that next time when the application is run these selections could be restored.
For accessing the registry we will use the Registry and RegistryKey classes available in the Microsoft.Win32 namespace. Naturally, we will have to add a using statement for this namespace. Create a Windows Application and design a form as shown in the following screen shot.
Also add the OpenFileDialog and ColorDialog controls to the form. Name them as file and color respectively. The 'Choose Bitmap' button allows user to select an image file through the 'Open' file dialog. The 'Choose Color' button lets the user to select a foreground color through the standard 'Color' dialog.
To store the information of our application we would create a tree structure of two sub-keys-Theme and Settings under the 'HKEY_LOCAL_MACHINE/SOFTWARE' key. The Settings sub-key would contain all the values and data. Each key we want to access is represented by an object of the RegistryKey object. So, create the objects that would represent the SOFTWARE key and two user-defined keys as shown below.
RegistryKey skey, tkey, rootkey ;
To write anything in registry keys, we must open them. If keys are not created, we must first create them. We would do this in constructor after a call to the InitializeComponent( ) method. Add the following statements to the constructor.
rootkey = Registry.LocalMachine.OpenSubKey ( "SOFTWARE", true ) ; tkey = rootkey.CreateSubKey ( "Theme" ) ; skey = tkey.CreateSubKey ( "Settings" ) ;
In a file system, root drives like C:\ or D:\ are the root directories. In registry, there are root keys called 'registry hives' or 'base keys'. There are seven such registry hives. The Registry class provides seven fields of type RegistryKey representing the seven registry hives. The LocalMachine field represents the HKEY_LOCAL_MACHINE base key. The OpenSubKey( ) method opens the key. Since we have called the method using LocalMachine field, it would open the SOFTWARE key of Local Machine hive. By passing true as the second parameter we have specified that the key is to be opened for writing. Next, we have created the 'Theme' sub-key in the SOFTWARE key and 'Settings' in the 'Theme' sub-key. We have called the CreateSubKey( ) method for this.
Now let us add handlers for the buttons. The b_bitmap_Click( ) handler gets called when 'Choose Bitmap' button is clicked.
private void b_bitmap_Click ( object sender, System.EventArgs e )
{
file.Filter = "Image Files (*.bmp,*.gif,*.jpg)|*.bmp;*.gif;*.jpg||" ;
if ( file.ShowDialog( ) == DialogResult.OK )
{
BackgroundImage = Image.FromFile ( file.FileName ) ;
skey.SetValue ( "Image", ( string ) file.FileName ) ;
}
}
Here, we have displayed the 'open' dialog. We have applied filter so that only image files would get displayed. If the user selects file we have changed the background image of the form and also written the file path as data of the 'Image' value in the 'Settings' sub-key. We have called the SetValue( ) method for writing the data. The SetValue( ) method takes the value name and data as parameters. The data needs to be type-casted in suitable type as the method takes parameter of type object.
The b_color_Click( ) method gets called when clicked on the 'Choose Color' button. The handler is given below.
private void b_color_Click (object sender, System.EventArgs e )
{
if ( color.ShowDialog( ) == DialogResult.OK )
{
ForeColor = color.Color ;
skey.SetValue ( "Red", ( int ) color.Color.R ) ;
skey.SetValue ( "Green", ( int ) color.Color.G ) ;
skey.SetValue ( "Blue", ( int ) color.Color.B ) ;
}
}
Finally add the code to write size and position to the registry. This code should get executed when the window is closed. So, we would add this code in the Dispose( ) method. The Dispose( ) method is given below.
protected override void Dispose ( bool disposing )
{
skey.SetValue ( "Height", ( int ) Height ) ;
skey.SetValue ( "Width", ( int ) Width ) ;
skey.SetValue ( "X", ( int ) DesktopLocation.X ) ;
skey.SetValue ( "Y", ( int ) DesktopLocation.Y ) ;
skey.Close( ) ;
tkey.Close( ) ;
rootkey.Close( ) ;
// AppWizard generated code
}
After writing the values we have closed the keys by calling the Close( ) method.
Our writing part is now over. We want that when application is executed the next time, the form should get displayed the same way as it was before closing. To read the information from registry, we have written a user-defined method readsettings( ).
public void readsettings( )
{
rootkey = Registry.LocalMachine.OpenSubKey ( "SOFTWARE", false ) ;
tkey = rootkey.OpenSubKey ( "Theme" ) ;
if ( tkey == null )
return ;
skey = tkey.OpenSubKey ( "Settings" ) ;
string f = ( string ) skey.GetValue ( "Image" ) ;
if ( f != null )
BackgroundImage = Image.FromFile ( f ) ;
Try
{
int r = ( int ) skey.GetValue ( "Red" ) ;
int g = ( int ) skey.GetValue ( "Green" ) ;
int b = ( int ) skey.GetValue ( "Blue" ) ;
ForeColor = Color.FromArgb ( r, g, b ) ;
Height = ( int ) skey.GetValue ( "Height" ) ;
Width = ( int ) skey.GetValue ( "Width" ) ;
int x = ( int ) skey.GetValue ( "X" ) ;
int y = ( int ) skey.GetValue ( "Y" ) ;
DesktopLocation = new Point ( x, y ) ;
}
catch ( Exception e )
{
return ;
}
}
Here, firstly we have opened the keys. When the application is run for the first time, keys are not created. So, we have checked for the null reference in the RegistryKey object. The statements written thereafter read the data from registry and use it.
Call this method from the InitializeComponent( ) method after the initialization code.



