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!

Next: Final Words >>
More C# Articles
More By Barzan "Tony" Antal