Working with Windows Registry in VB.NET – Part 2: Writing to the Registry

Welcome to the second part!

In this part I’ll teach you how to write stuff away inside the Windows Registry.


Proceed to the summary of this post

*NOTE: For the remainder of this 5-part blog entry, i’ll be using the HKEY_LOCAL_MACHINE hive to read and write from the registry.

Ok, let’s say we got an application called “MyApp” and we have certain settings we would like to store inside the Windows Registry.
Settings such as the version of the program, the author (programmer) and a serial for the program.

All software settings are stored within the SOFTWARE subkey from our used hive, so it’s obvious that our settings should be stored within HKEY_LOCAL_MACHINE\SOFTWARE\MyApp

A special note regarding this:

Always keep in mind the following:
HKEY_LOCAL_MACHINE\SOFTWARE\… for 64-bit applications on a 64-bit operating system OR for 32-bit applications on a 32-bit operating system
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\… for 32-bit applications on a 64-bit operating system

Code-wise this isn’t really an issue, since it’ll automaticly store data within the correct subkey. Only if you plan to manually lookup and/or change settings during the development
of your program you’ll have to look under the correct subkey. I don’t want you freaking out like “Omg, it stored the settings somewhere hidden in my Registry and now I can’t find it!”

I myself am using a 64-bit operating system. Keep that in mind for the remainder of this post.

Ok, now that we have that cleared out, let’s continue shall we? 🙂

The .NET framework provides us with 2 base classes located within the Microsoft.Win32 namespace to interact with the Registry.

Microsoft.Win32.Registry
Microsoft.Win32.RegistryKey

So you might want to start off with using the Microsoft.Win32 namespace in your project:

'Importing the Microsoft.Win32 Namespace
Imports Microsoft.Win32

Once you’ve done that, it’s time to work with those 2 classes.
We’ll start off by declaring a variable called oRegKey as a RegistryKey:

'Declaring our Registry key
Dim oRegKey As RegistryKey

After that we define where the key has to work:

'Define the subkey we're going to use in writeable mode
oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)

Note: the “true” defines wether we can write to the subkey or not.
Alright, so we’re working inside “HKEY_LOCAL_MACHINE\SOFTWARE”

A good habbit is to check wether the subkey you plan on using (in our case MyApp) already exists or not. I assume you’re more then capable of writing that small code snippet yourself.
Once we’re sure that the subkey doesn’t exist, let’s create it!

To create a subkey, use the following code snippet:

'Create a SubKey for our program
oRegKey.CreateSubKey("MyApp")

Now, in order to store our settings under that subkey, we have to say that our oRegKey has to be working inside that subkey:

'Define our own SubKey to work with in writeable mode
oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MyApp", True)

Once we’ve done that, we can start storing our settings!

'RegistryKey.SetValue example
oRegKey.SetValue(Name As String, Value As Object)

So in our case:

'Store values
oRegKey.SetValue("Version", "1.0")
oRegKey.SetValue("Author", "Dirk Schuermans")
oRegKey.SetValue("Serial", "A1B2C3D4E5F6G7H8I9")

Once you’ve run those commands, you can find those settings stored in your Registry!

HKEY_LOCAL_MACHINE\SOFTWARE\MyApp\… for 64-bit applications on a 64-bit Operating System or for 32-bit applications on a 32-bit Operating System
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MyApp\… for 32-bit applications on a 64-bit Operating System

Finally we’ll close the regkey with the following command:

'Close the RegistryKey
oRegKey.Close()

There we go! You know posses the knowledge of how to write to the Windows Registry!
Please keep in mind that different parts of the Registry require different security permissions. The 2 software subkeys are usually writeable by regular users aswell.


POST SUMMARY

'Import correct namespace
Imports Microsoft.Win32

'Procedure to store stuff in the Registry
Public Sub fnWrite()
	'Declare variables
	Dim oRegKey As RegistryKey

	'Set the subkey to work with
	oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)

	'Create a new subkey
	oRegKey.CreateSubKey("MyApp")

	'Set the subkey to work with
	oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MyApp", True)

	'Create new values
	oRegKey.SetValue("Version", "1.0.0.0")
	oRegKey.SetValue("Author", "Dirk Schuermans")
	oRegKey.SetValue("Serial", "A1B2C3D4E5F6G7H8I9")

	'Close the key
	oRegKey.Close()
End Sub

– Continue to Part 3

3 thoughts on “Working with Windows Registry in VB.NET – Part 2: Writing to the Registry”

  1. Hi… I’m also puzzled about the registry and have never understood how to maintain it or keep it from corrupting my system somehow.

    I’ve recently had a very frustrating email exchange with Mihal Roman from Spamfighter. Firstly he told me that they have no intention of fixing the bugs and various flaws in the antispam software, saying that this is how the software is intended to work and there’s no need to improve it or release any bug-fixes in the future. It’s good enough, he claimed. (I was complaining about spam emails that weren’t being blocked, despite my marking them as spam and adding their addresses and domains to the black list). He then said it’s perfectly OK for the Spamfighter software to use the registry to store and maintain the black and white lists.

    Is that right?

    It means that every time I receive email (and I get around 1,000 emails a day), Spamfighter is writing to the registry. And so far there are about 20,000 entries for all the black-listed emails that I’ve already imported.

    Isn’t there a risk of corrupting the registry (particularly if there are outstanding bugs and security holes in the software that aren’t being maintained)? I would have thought that the registry needs to be backed up before any major changes (like installing new software). But if the registry is being used as a private database file for an application then almost anything could cause a serious system crash – maybe something as little as a power failure in the middle of receiving emails and/or marking an email as spam or not-spam.

    Should I be worried? Should I uninstall Spamfighter?

    1. Hello Gary,

      I don’t have any experience with Spamfighter.

      Windows registry should be used by applications to store small amounts of information, such as author, version etc.
      Pretty much data you don’t want inexperienced users to manually change. It’ll be safer to assume users won’t change those settings when they’re tucked away in the windows registry as opposed to being plainly visible by means of an configuration file that resides in the same folder (or subfolder) of the executable.

      If you want my opinion, uninstall it. There are probably better alternatives out there.
      I can’t tell for sure if it’ll have an impact on your system’s stability, having that many registry entries. Probably not though.

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.