In this article I will show how to read and use in your .Net C# application informations stored in the Windows registry. First of all let’s see the easiest way to store some information in the server registry.

You can use the “regedit” command to edit the register or you can directly use a register file. Today I’ll show this option.

C Sharp

C#

First of all create a file and call it, say, “myApp1.reg” where myApp1 it will be the name you want to give to the Registry Key of your app. If you want a different name, change it but be careful and backup you registry before to take any action on the Registry. You’ll overwrite eventually key with the same name.

Let’s see now what to write inside our “myApp1.reg”

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\YourBrand\myApp1]
"aPath"="E:\\myFooFolder\\"
"aTimer"="30"
"aServer"="localhost"
"aVariable"="begood"
"whateveryouwant"="desired-value-here"

… and so on.

You can store any key you want and give the relative value. The string on the left part will be also the variable you’ll use in your application. The right part is the value.
Now save the file and double click on it. Confirm (if you are sure of what you are doing) and take a look on how to retrieve these information in your application.

First of all you have to add the line

using Microsoft.Win32;

on the top of your file, before the class declaration.

Second you need to get the registry Key you create before

private RegistryKey yourAppKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\YourBrand\\myApp1", false);

Third you can create and assign the variables of your application with the value take from the registry

 
private string use_aPath;
private int use_aTimer;
private string use_aServer;
private string use_aVariable;
private string use_whateveryouwant;

use_aPath = yourAppKey.GetValue("aPath").ToString();
use_aTimer = yourAppKey.GetValue("aTimer").ConvertTo<int>(0);
use_aServer = yourAppKey.GetValue("aServer").ToString();
use_aVariable = yourAppKey.GetValue("aVariable").ToString();
use_whateveryouwant = yourAppKey.GetValue("whateveryouwant").ToString();

Et voilà from now you have all you variables retrieved from the registry and ready to be used.