C#
  Home arrow C# arrow Page 4 - Working with Windows Registry in C#
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Working with Windows Registry in C#
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2008-03-05

    Table of Contents:
  • Working with Windows Registry in C#
  • The Basics
  • Let's Do It!
  • More Techniques
  • Final Words

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Working with Windows Registry in C# - More Techniques
    (Page 4 of 5 )

    We haven't really discussed the details of the OpenSubKey() method. On the previous page, it accessed the subkey only in read-only mode. That's because it has two overloads. The first only specifies which key to open; in this case, it opens and returns the subkey as read-only. However, we can add another bool parameter (boolean - true or false) that explicitly specifies the desired write mode.

    If we want to modify the contents of an existing subkey, we use it accordingly:

    regkey = Registry.CurrentUser;

    regkey = regkey.OpenSubKey("SoftwareTutorial", true);

    Of course, we cannot forget about error handling, but that has already been covered previously. Next, we are going to modify the data value of one of our values located in HKCUSoftwareTutorial. I have chosen the "DS" value and changed its data from "Dev Shed" to "Edited Dev Shed." We do this using the SetValue() method. Follow along:

    regkey.SetValue("DS", "Edited Dev Shed");

    regkey.Flush();

    Notice that I've also added the Flush() method, which writes all of our modifications to the registry. This is important in case you want to peek into your Windows registry with regedit to see whether the modifications were successfully completed. The SetValue() method works like this: you specify the name of the value that's going to be modified and then the new data for that value. It's that simple.

    After this stage, you can print out all of the values located in our Tutorial subkey using the technique presented before. In my case, the output was the following:

    DS contains Edited Dev Shed

    AF contains ASP Free

    SC contains SEO Chat

    DA contains Dev Articles

    DH contains Dev Hardware

    Now comes the deleting part. We are going to introduce three new methods. The first is DeleteSubKey(). It deletes the specified subkey. The second is DeleteSubKeyTree(). It's yet again self-explanatory because it deletes the specified tree of subkeys. The last one is DeleteValue(),which obviously deletes the specified value. Check out their syntax below.

    public void DeleteSubKey(string subkey);

    public void DeleteSubKeyTree(string subkey);

    public void DeleteValue(string name);

    Let the fun begin! We are going to "destroy" our Tutorial subkey by deleting a value from it first, then deleting the NewSubKey located in Tutorial subkey, and then complete this process by deleting our entire Tutorial subkey as a whole.

    Here's the code segment. This won't be included in the attached source code sample because it would delete everything (only the Tutorial subkey, in fact) at the end.

    regkey = Registry.CurrentUser;

    regkey = regkey.OpenSubKey("SoftwareTutorial", true);

    regkey.DeleteValue("DS");

    regkey.DeleteSubKey("NewSubKey");

    regkey.Close();

    regkey = Registry.CurrentUser;

    regkey = regkey.OpenSubKey("Software", true);

    regkey.DeleteSubKeyTree("Tutorial");

    regkey.Close();

    As you can see from the block of code above, the deleting process was done in two stages. First we opened the HKCUSoftwareTutorial subkey in writable mode and then deleted the DS value and its NewSubKey subkey. After this, we closed it and reopened it again in writable mode, but now as its parent subkey, HKCUSoftware. And then using the DeleteSubKeyTree() method, we removed our Tutorial subkey in its entirety.

    We have just finished learning how to read, write, modify, and delete from/to the registry. This means that we are prepared to "work" and manipulate with the Windows Registry any time. Here I am attaching the sample source code in a fully commented format, so it helps you understand and grasp the specific concepts. Hope it helps!

    More C# Articles
    More By Barzan "Tony" Antal


       · Thanks for reading this tutorial. I hope you've found it informative and education....
       · Clean, crisp to the point introductory article, keep it up.Thanks
     

    C# ARTICLES

    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained
    - A Look at C# File and FileInfo Classes
    - A Look at C# Directory and DirectoryInfo Cla...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway