C# Registry Operations
When working with the registry in C#, you have the option to access it using either the functions provided by C# or the registry classes available in the .NET Framework. The .NET Framework offers two key classes within the Microsoft.Win32 namespace for registry operations: the Registry class and the RegistryKey class.
The Registry class provides access to the base registry keys through shared public methods. These methods allow you to perform common operations on the registry without explicitly opening or closing registry keys. Here are some of the important shared methods provided by the Registry class:
- HKEY_CURRENT_USER
- HKEY_USERS
- HKEY_LOCAL_MACHINE
- HKEY_CLASSES_ROOT
- HKEY_CURRENT_CONFIG
Each key has many subkeys and may have a value.
Once you have access to a base registry key using the Registry class, you can further manipulate the registry by working with instances of the RegistryKey class. The RegistryKey class provides methods and properties to perform operations like creating, deleting, reading, and writing registry keys and values. The Registry class provides base registry keys as shared public methods:
Creating a Registry Entry
Above code shows how to create a subkey under HKLM\Software called AppReg.
Deleting a Subkey
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.
Full Source C#Conclusion
Using the Registry class and the RegistryKey class from the Microsoft.Win32 namespace, you can access and manipulate registry settings in your C# applications. This allows you to store and retrieve application-specific and user-specific settings efficiently and reliably.