Manipulating the Registry in VB.NET Posted by: Xav in Untagged  on
I'm back again! Now, if you've read my other blog, which can be found here:
http://blog.codecall.net/component/myblog/The-Windows-Registry-in-.NET.html
... you'll know all about the Registry. Now it's time to actually read and write to it!

Note: The CODE tags seem to remove my backslashes (). Hopefully you can work out where they go.

Objects
It is possible to use GetSetting() and SetSetting() to set or retrieve values, but this is very limiting in many ways. As mentioned afore, the best way is to use the My.Computer.Registry object, which is .NET-formulated and specific to VB. However, the Registry object is still available in other languages, such as C#.

The My.Computer.Registry object contains many objects. It has one object for every hive in the registry. Here are their names in relation to the actual hives:

 Property Hive
 ClassesRoot HKEY_CLASSES_ROOT
 CurrentConfig HKEY_CURRENT_CONFIG
 CurrentUser HKEY_CURRENT_USER
 LocalMachine HKEY_LOCAL_MACHINE
 Users HKEY_USERS

Creating keys
It is easy to create keys. First, you reference the property corresponding to the hive you wish to put the key in. Then, you call the CreateSubKey() method, with one parameter - the name of the key to create. For example:


 
  1. My.Computer.Registry.CurrentUser.CreateSubKey("MySettings")


If you run this statement, a new key called "UserSettings" is created under the HKEY_CURRENT_USER hive. However - don't do this. Software hardly ever creates keys directly under a hive. If you explore the Registry Editor, you can see which keys are directly under the hive. However, you should use the Software key. All computers have a Software key, and you should store your company name/software underneath that. This would be better:


 
  1. My.Computer.Registry.CurrentUser.CreateSubKey("SoftwareXavsSoftwareMyProgram")


See how we have passed in a whole directory structure. Run the program now - this is what it would do:
  1. First, the program would look for the Software key. It would be found.
  2. Then, it would look for the XavsSoftware key underneath that. If it was found, then fine. If not, it would be created.
  3. The MyProgram subkey would be created.
Note that if VB finds that a key already exists, it does not overwrite it.

Deleting keys
There are two methods to use - DeleteSubKey() and DeleteSubKeyTree(). The difference between them are the DeleteSubKeyTree() deletes not only the key, but all the subkeys beneath it. Be careful not to delete something important!

Here's what we would do:


 
  1. My.Computer.Registry.CurrentUser.DeleteSubKey("SoftwareXavsSoftwareMyProgram")


Note that DeleteSubKey() throws an exception if the key does not exist. Make sure you account for this in your programs.

Getting/Setting Values
Keys are only as useful as the values they contain. It is harder to manipulate the values than the keys, but this does not mean .NET doesn't take the pain out of things!

We need to use SettValue() and GetValue(). Hopefully you know which is which! There are three parameters in SetValue():

SetValue ( path, name, value )

You need to specify the hive name as well as the directory. You do not need the data type, as VB decides this automatically. So, to set the "SaveOnExit" property in our Xav's SoftwareMyProgram key, use this code:


 
  1. My.Computer.Registry.SetValue("HKEY_CURRENT_USERSoftwareXavsSoftwareMyProgram", "SaveOnExit", "True")


And that, my dear friends, sets the SaveOnExit value to "True". Notice how you don't specify the hive using a property, but rather in the actual path name.

And that leaves one last function:

GetValue ( path, name, defaultvalue )

This method works in a similar way to SetValue(), except it returns a value - the value you choose within a key in the Registry. Like this:


 
  1. My.Computer.Registry.GetValue("HKEY_CURRENT_USERSoftwareXavsSoftwareMyProgram", "SaveOnExit", "False")


If the SaveOnExit value is found, it is returned. If not, "False" is returned. This prevents the need for an exception to be thrown, if the key or value does not exist.

Conclusion
The Registry needn't be scary, if used properly. Just remember to treat a user's registry carefully - it isn't your personal storage space. If you need to store large amounts of data as opposed to small settings, then a text file may be a better idea. Both are easy to implement, thanks to the glory of .NET. Long live Microsoft.

Trackback(0)
feed1 Comments
Jordan
July 13, 2008
Votes: +0

Another excellent read! Looking forward to the next one.

report abuse
vote down
vote up

Write comment
 
 
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger
 

security image
Write the displayed characters


busy