C# .NET Registry-based configuration base class

C# .NET Registry-based configuration base class
Photo by Headway on Unsplash

I usually find myself wanting to be able to configure my application without having to change the underlying code. And although Visual Studio allows you to create application settings inside the IDE (saved to a .exe.config file), I would prefer to use the registry to hold my applications configuration.

My solution was to make a base registry configuration class that allows me to create/modify registry keys.

So where do we start?

Lets create our class! I’ll be calling it RegistryConfig

Now we have created our class lets add some properties and a constructor.

So here we have read-only properties that are set from parameters taken by the constructor. Because this is an abstract class we should mark any constructors we make as either protected or private.

The methods

So now we have to add a few methods! Firstly I shall add a function for parsing the Hive property that was set when the class was initialized. This function will return a RegistryKey object that we can use to access and the RootKey and the values in that subkey.

Now we must create the methods for getting and setting the values within our class.
These methods will use the previously created function to access the RegistryKey object we need to access the subkey.

A handy function we can create from the GetValue method, is one where we can check if a value exists!

The whole thing!

Here is what the class should look like.