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

Quick Registry class in C#

Rate me:
Please Sign up or sign in to vote.
3.09/5 (17 votes)
5 Oct 20023 min read 87.1K   657   26   5
A C# registry class that allows quick operations

Introduction

I'm currently working on a project that requires using the setup facilities provided by the .NET developer studio and I used the built in registry functionality to create the registry keys when the project is installed. This proved to be somewhat unsatisfactory, because the setup I added was to create one key below another in the format "Default Company Name\Project\Computers". As it turned out, on reflection I dropped the "Computers" key and just used the project key, yet when I was developing the save and load code for the program, I couldn't open the "Project" key. It was there in the registry but nothing would open it until I realized that somehow the key had been saved not as "Project" but as "Project ", with an extra space creeping in from somewhere, which is probably why the second key was not created and why I couldn't open it. At first I thought that I'd leave it but then as the project is not just demo code ( although it will be published with source and an article ) I thought what if the bug is fixed in an update or later version of .NET and my project refuses to work because all the registry code is looking for the "Project " key. This plainly wasn't good enough so I decided to write the registry functions into the code but being lazy and having already written it a couple of times with proper exception checking I decided that it was time to write it all into a class that would do all the error checking for me and let me get on with developing the application.

The resulting class is not an attempt to completely cover all the possible aspects of what can be done with the registry but covers all the most used functions such as creating sub-keys from the main key that has been opened, opening keys off the current key and deleting keys as well as the sub-key tree; there is also some rudimentary backwards movement that is by no means comprehensive, but should prove useful for most occasions when you want to create or edit a key that is off your main key and then move back to the main key again.

Code listing

The demonstration code is meant to be used with the debugger and break point has been set at the start of the test code.

C#
QuickRegistry reg = new QuickRegistry();

/// create a new key off the current user 
/// key using return value directly
if( reg.CreateKey( "HKEY_CURRENT_USER", 
   "TestCompany" ) == false )
MessageBox.Show( reg.ErrorMessage );

/// create a project key using QucikRegistry error check
reg.CreateKey( "ProjectName", true, true );
if( reg.Error == true )
MessageBox.Show( reg.ErrorMessage );


/// create a couple of values for the project
reg.SetValue( "value1", "test String" );
if( reg.Error == true )
MessageBox.Show( reg.ErrorMessage );

reg.SetValue( "value2", 2 );
if( reg.Error == true )
MessageBox.Show( reg.ErrorMessage );

string strTest1 = ( string )reg.GetValue( "Value1" );
if( reg.Error == true )
MessageBox.Show( reg.ErrorMessage );

int nTest2 = ( int )reg.GetValue( "Value2" );
if( reg.Error == true )
MessageBox.Show( reg.ErrorMessage );

/// move back
reg.RevertToPrevious();
if( reg.Error == true )
MessageBox.Show( reg.ErrorMessage );

/// delete everything in one go
reg.DeleteKey( false, "ProjectName", true );
if( reg.Error == true )
MessageBox.Show( reg.ErrorMessage );


/// At this point the testcompany key still exists
reg.DeleteRootKey( "HKEY_CURRENT_USER", 
                  "TestCompany", false );
if( reg.Error == true )
MessageBox.Show( reg.ErrorMessage );

This is the complete test code and as you can see all it does is create the Default Company name key off the current user registry key before creating a project key off that and adding a couple of values, that are then retrieved from the registry using the GetValue function, before stepping back and deleting the project name sub tree and then deleting the current root key off the current user key.

In practice the deletion of the keys could have been done with one delete call but it's demo code, so I was trying to use as many of the functions as possible. One further thing is that when I tried using this code in my application it showed that I'd missed a couple of required functions these being the Close function and a function to return the sub-keys from a key. These have been added to the main code, although they don't feature in the demo code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralProblems with int values... Pin
ThaNerd13-Sep-04 9:16
ThaNerd13-Sep-04 9:16 

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.