How to Read and Write from Registry Key Values
If you are coding or programming in .NET environment and if you need to deal with system registry within your application, then you should use Microsoft.Win32 namespace in your .Net project. Microsoft.Win32 namespace provides the necessary classes and methods that enables us to manipulate the system registry.
The registry is the main information depot where information about the operating system and the applications running on this operating system exists. The data stored in the registry is arranged in an hierarchical format.
The RegistryKeys are the units of the hierarchical data storage in system registry. Each key may have subkeys. Keys may have multiple key-value pairs associated with them. Values associated with key can be edited, deleted or new values can be created related with that key.
If you are creating an application using .NET platform and want to store some information in the system registry you can create store your key value pairs in system registry in a format similar to "My Computer\HKEY_LOCAL_MACHINE\SOFTWARE\KODYAZ\RegistrySampleApplication" where instead of "KODYAZ" you can use your company name and you can replace "RegistrySampleApplication" with your application name.
The main classes that enables the developers to create and edit registry keys and values are "Registry" and "RegistryKey" classes.
Registry class supplies the base RegistryKeys that access values and subkeys in the registry.
RegistryKey class represents a key level node in the windows system registry.
Some of the properties of RegistryKey are :
Since Registry is the base class which supplies the root key that access values and subkeys in the registry, our application should begin with the Registry value.
Registry.LocalMachine contains information related with the local computer. These values are stored in the windows registry under HKEY_LOCAL_MACHINE.
Registry.LocalMachine contains five subkeys. One of them is "Software" which contains configuration and installation information about installed software on that computer.
If you want to create a registry in the system registry for "SOFTWARE\KODYAZ\RegistrySampleApplication" under "HKEY_LOCAL_MACHINE" then you should get the root registry key by the sample below,
Then, to create the "SOFTWARE\KODYAZ\RegistrySampleApplication" key hierarchy, use the following sample code statements
If you are not sure whether the subkey exists or not, you may want to test if it is already existing in the registry by the method shown in the sample below,
Note that OpenSubKey method of the RegistryKey class takes a boolean parameter named "writable". This parameter states that the key is opened for edit. So if the key in the sample exists in the system registry, then it is opened editable.
The above sample is a simple value creating or editing which belongs to the registrySubKey in the registry.
When you get a RegistryKey, you may want to list its sub keys and values.
You will find a Visual Studio 2003 VB.NET project named "RegistrySampleApplication" in the files section which contains samples to list, create and edit Registy Keys, Values and Value Data.
You can either download the sample VB.NET project by this link.
Sample Registry Editing Application in VB.NET
The following is no more valid : Sample Registry Editing Application in VB.NET
Here is a screen shot from the sample VB.NET registry application