Jan
28
2010

Accessing the Windows registry with C#

I wanted to write a little bit about using the Windows registry from a .NET language.  I’ve been working on a small desktop application and I wanted to store some state information that is used upon starting the application.  The Windows registry made sense in this particular instance.  Naturally, I turned to the Google machine to find some code examples on access the registry only to find that most of the code examples didn’t work at runtime, at least under the .NET Framework 3.5.  I corrected the problem and am sharing the code with anyone interested.

The Windows registry is something like a central database used by the Windows operating system and most applications for storing all sorts of information that can be easily retrieved.  It is organized into tree like structures with keys and values.  It’s important to understand how the registry is organized so you can make an informed decision on where you want to create your keys.  It’s also a real easy way of fouling up your operating system, so BE CAREFUL!

In this example, I wished to store a string value in a key that can be retrieved every time the application starts and a means of allowing the user to update the value at any time.  I accomplished these using two methods in the main class of the application.  For step is to include the appropriate namespace for the Registry classes are located; the namespace is Microsoft.Win32.

using Microsoft.Win32;

Now for the methods:

/// <summary>
/// Check the registry key for the string value.
/// </summary>
/// <returns>string</returns>
private string GetRegistryKey()
{
RegistryKey key Registry.CurrentUser.OpenSubKey("Software\\cSpotInterWorks\\SubkeyName");
if (key == null)
{
key = Registry.CurrentUser.CreateSubKey("Software\\cSpotInterWorks\\SubkeyName");
key.SetValue("ValueName", String.Empty);
}
string myString = key.GetValue("ValueName").ToString();
key.Close();
return myString;
}
/// <summary>
/// Set the registry key with the value string.
/// </summary>
/// <param name="myString">string: A string value.</param>
private void SetRegistryKey(string myString)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\cSpotInterWorks\\SubkeyName", true);
if (myString.Length != 0)
 {
 key.SetValue("ValueName", myString);
 }
 key.Close();
}

My application calls GetRegistryKey() in the constructor of the main application class.  The OpenSubKey method is called to open the key at the specified location.  In this case, I used the Software tree with our company name, and the sub-key name.  If the sub- key hasn’t been located the type “key” will be null.  So I check for the null and create the sub-key using the CreateSubKey method.  After the sub-key has been created I populate it with an empty value for use later.

Many code examples do not include the CreateSubKey method.  If you do not include it, a null object reference exception will be thrown.   So be sure to create the sub-key if it doesn’t exist when you open it.

The second method SetRegistryKey(string myString) is used to change the value of the sub-key.  Once again, declare a type that references the sub-key you wish to access.  Use the SetValue method with the value name and the string to be stored.  I didn’t bother checking to see if the sub-key exists as I handled that in the GetRegistryKey() method and never call the SetRegistryKey method first.  That’s entirely up to you.

There’s plenty of written material on the subtleties of the registry.  I hope at least this will get you pointed in the right direction!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • Slashdot
  • StumbleUpon
  • Technorati
Written by smsullivan in: .NET Framework | Tags: ,

No Comments »

RSS feed for comments on this post. TrackBack URL


Leave a Reply

Powered by WordPress | Theme: Aeros 2.0 by TheBuckmaker.com