C# Registry Operations
The registry is an optimal location for saving information about your application as well as individual user settings.
The Registry comprises a number of logical sections called Hives. The following are the predefined keys that are used by the system.
HKEY_CURRENT_USER HKEY_USERS HKEY_LOCAL_MACHINE HKEY_CLASSES_ROOT HKEY_CURRENT_CONFIG
Each key has many subkeys and may have a value.
When programming in C#, you can choose to access the registry via either the functions provided by C# or the registry classes of the .NET Framework. The operations on the registry in .NET can be done using two classes of the Microsoft.Win32 Namespace: Registry class and the RegistryKey class. The Registry class provides base registry keys as shared public methods:
Creating a Registry Entry
rKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", true); rKey.CreateSubKey("AppReg");
Above code shows how to create a subkey under HKLM\Software called AppReg.
Deleting a Subkey
rKey = Registry.LocalMachine.OpenSubKey("Software", true); rKey.DeleteSubKey("AppReg", true);
The following C# program shows how to create a registry entry , set values to registry entries, retrieve values from registry and delete keys on Registry. Drag and drop four buttons on the form control and copy and paste the following source code.