Many programs use the registry to store persistent data such as settings or user defined variables – which are really just settings, too – which can be retrieved whenever the program needs them. These settings survive, in most cases,  multiple loadings and unloadings of the program, uninstallation of the program, reinstallation of the program, update or upgrading of the program, and program crashes. This makes the registry a very valuable place to store these settings!

If you’re creating your own program, maybe you’ve already considered how you were going to save the settings. Well, saving your program’s settings to the registry is a piece of cake, and I’m going to show you how.

The following code will allow you to store and retrieve settings from the registry:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using Microsoft.Win32;
using System;
using System.Text;

namespace ProgramName
{
    // LocalSettings will allow you to access the registry
    public class LocalSettings
    {
        // This path is important, as it will be the new home for your
        // program's settings. Registry.CurrentUser is HKCU.
        // It is recommended to limit settings to the HKCU.
        // DON'T FORGET TO CHANGE {YOUR PROGRAM NAME HERE}
        private static string PATH = Registry.CurrentUser + "\\SOFTWARE\\{YOUR PROGRAM NAME HERE}\\";
       
        // This will save all keys to the registry, regardless of type.
        public static void saveKey(String keyName, object keyVal)
        {
            Registry.SetValue(PATH, keyName, keyVal);
        }
       
        // This will load any key that has been stored as a string.
        // This will return the loaded key's value as a string.
        public static string loadKeyString(String keyName)
        {
            object keyVal = Registry.GetValue(PATH, keyName, null);
            if (keyVal == null)
            {
                return "";
            }
            else
            {
                return (string)keyVal;
            }
        }
       
        // This will load any key that has been stored as boolean.
        // This will return the loaded key's value as true or false.
        public static bool loadKeyBool(String keyName)
        {
            object keyVal = Registry.GetValue(PATH, keyName, null);
            if (keyVal == null)
            {
                return false;
            }
            else
            {
                return bool.Parse((string)keyVal);
            }
        }
    }
}

This method of storing and retrieving settings is only useful for saving individual keys and values, so if your program needs to save and load a lot of settings, you may wish to explore other options. To save a key and value to the registry, you would use LocalSettings.saveKey({NAME}, {VALUE}); where {NAME} is the Key name and {VALUE} is the value to be saved. The value can be a string or true/false stored as a string with a value of “true” or “false”. To retrieve a saved value of a stored string, use {X} = loadKeyString("{NAME}"); where {NAME} is the name of the key you are getting the value of, and {X} is any object you want to hold the value you are retrieving. If you are retrieving a stored boolean value, use {Z} = loadKeyBool("{NAME}"); where {NAME} is the name of the key you are getting the value of, and {Z} is any object you want to hold the value you are retrieving. Keep in mind, when using variables to store the values you just retrieved, the variable {X} above must be created as string {X}; since the value of the key is a string, and the variable {Z} above must be created as bool {Z}; since the value of the key is true or false.

With any luck I have been at least some help to someone, or at the very least I’ve hopefully inspired someone – maybe you – to find a better way to access the registry and let others know how to do it!