Click here to Skip to main content
13,344,765 members (52,577 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 5 Oct 2002

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


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
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.


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


About the Author

Anthony Roach
Web Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralProblems with int values... Pin
ThaNerd13-Sep-04 10:16
memberThaNerd13-Sep-04 10:16 
GeneralRemote Registry Changes Pin
balajik20-Feb-03 7:54
memberbalajik20-Feb-03 7:54 
I am trying to find this for a long time..
Is there a way to change the registry key remotly or by using WMI.

Generalzerg Pin
Anonymous7-Oct-02 14:29
sussAnonymous7-Oct-02 14:29 
GeneralRe: zerg Pin
Kinlan27-Oct-03 3:57
memberKinlan27-Oct-03 3:57 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 6 Oct 2002
Article Copyright 2002 by Anthony Roach
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid