Quick Registry class in C#

By , 5 Oct 2002

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.

A list of licenses authors might use can be found here

 Anthony Roach Web Developer United Kingdom Member
No Biography provided

Votes of 3 or less require a comment

 Search this forum Profile popups    Spacing RelaxedCompactTight   Noise Very HighHighMediumLowVery Low   Layout Open AllThread ViewNo JavascriptPreview   Per page 102550
 First Prev Next
 Problems with int values... ThaNerd 13 Sep '04 - 9:16
 For my configuration, i had made a getter/setter member in a class, that got or set value to registry...   For some reason, my IDE didn't car about the type of value, in that case being an int... And for some more unobvious reason, the code compiled... But caused an exception at runtime.   Here's my get/set code...   public static int LastLoggedinUser { get { return lastLoggedinUser; } set { lastLoggedinUser = value; mr.Write("user", value); } } mr being an instance of the class...   On line 7, replace "value" with this to avoid the bug: mr.write("user", ((int)value).ToString());   PS: shortly i will extend the class to make it able to deal with two things : things like mr.write("foo\\bar", value) And, with DWORD type values...   Hope this helps   Life would be so much easier if we could look at the source code... Sign In·View Thread·Permalink
 Remote Registry Changes balajik 20 Feb '03 - 6: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.   Thanks Balaji Sign In·View Thread·Permalink
 zerg Anonymous 7 Oct '02 - 13:29