This is an article on using the Registry in C#. Having been looking around the web and on the MSDN libraries, I have determined that there is not a complete guide on using all the registry tools accessible using C#. So this article is going to cover all methods exposed to the user when developing in C#. Firstly I would like to say that I am very aware that the current consensus is that we should not be using the Registry to hold information about our programs anymore and that the ".NET" way is to store information in XML files. Well if that is you then you don't really need this article, however if you are accessing information from the registry then this is the source for you. Ok so lets get down to business.
When using the .NET framework in C# the user needs to add access to the Microsoft.Win32
namespace in order to access registry manipulation tools, this is done as shown in the below code:
using Microsoft.Win32;
Once this reference has been added along with the other, it gives us access to the elements involved within registry control. There are two main classes associated with this namespace, RegistryKey
and Registry
. These classes are used together to allow us to do pretty much everything in the registry.
Registry Class
The Registry Class itself on its own doesn't do a lot for us this is because the Registry Class merely represents the seven top-level nodes within the registry for us to access and manipulate. The Registry
class is powerful when used with the RegistryKey
class. However if you open up Visual Studio.NET and add the namespace above and then just type, "Registry." you will see the list of the seven subnodes (Shown below).
Registry Class name | Registry Value | What is does |
---|---|---|
Registry.ClassesRoot | HKEY_CLASSES_ROOT | Responsible for telling the shell how to handle different file formats |
Registry.CurrentConfig | HKEY_CURRENT_CONFIG | Represents the current session configuration |
Registry.CurrentUser | HKEY_CURRENT_USER | Represents the profile of the current user |
Registry.DynData | HKEY_DYN_DATA | Contains configuration data about dynamic data (i.e. in RAM) |
Registry.LocalMachine | HKEY_LOCAL_MACHINE | Contains the System Information (i.e. Hardware) |
Registry.PerformanceData | HKEY_PERFORMANCE_DATA | Contain information about the performance of applications |
Registry.Users | HKEY_USERS | A repository for information regarding users and their preferences. |
Now the we have identified what the Registry Class does and represents we can look at it's partner the RegistryKey
Class.
Comments