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

Quick Registry class in C#

, 5 Oct 2002
Rate this:
Please Sign up or sign in to vote.
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.

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

Share

About the Author

Anthony Roach
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralProblems with int values... PinmemberThaNerd13-Sep-04 10:16 
GeneralRemote Registry Changes Pinmemberbalajik20-Feb-03 7:54 
Generalzerg PinsussAnonymous7-Oct-02 14:29 
GeneralRe: zerg PinmemberKinlan27-Oct-03 3:57 

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 | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 6 Oct 2002
Article Copyright 2002 by Anthony Roach
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid