Working with Windows Registry using C#
Every one of us would have installed a trial version of software such as antivirus, game, licensed tool, etc. at some point in our life. But while doing it, have you ever noticed after the trial period ends, the software will magically stop working for you. Even when you try to uninstall and install the software again, it will shoot the same expired message. You must be wondering how this is possible. The answer to this is Windows Registry.
Once installed, the software takes the help of the Windows registry to provide all the necessary information in the registry. This information is then used by the software during runtime for several reasons such as security or for license purposes. In this post, we are going to explain what is Windows registry and how it can be accessed programatically.
Table of contents
- What is Windows registry?
- How to access the Windows registry?
- What is present in the registry editor?
- How to access the Windows registry programmatically?
- Wrapping Up
What is Windows registry?
Windows registry is a hierarchical database of settings. It stores the information, settings, options, and other values that are used by the hardware and software that are installed on your system.
In the initial version of Windows, there was a huge dependency on the configuration files where the program's configuration and settings were often stored. However, in the Windows registry, the details of an application are stored as a subkey.
How to access the Windows registry?
Registry Editor is a free registry editing tool that comes packaged with every known version of Windows right from Microsoft Windows 95 all the way to the latest Windows 10. It is a preloaded tool (no need to install) and can be accessed by executing the command regedit from the run (win+r) window.
The registry editor acts as the face of the registry and it is a user-friendly way to view and edit the registry information.
What is present in the registry editor?
The registry editor displays all the keys and values that are present in the Windows registry.
On opening the Windows registry editor, it displays the root keys which contain all the registry values. There are 5 common root keys, let's have a quick look at them -
HKEY_CLASSES_ROOT
It is also known as HKCR, it contains the information that helps Windows when a user requests to view the content of a drive, or open a certain type of file, etc.
It contains the information that in turn holds the file extension related information and OLE (Object Linking Embedding) information.
HKEY_CURRENT_USER
It is also known as HKCU, it forms the major part of the Windows registry. It contains configuration information of Windows and software that are installed which are specific to the current logged in Windows user.
Settings such as desktop wallpaper, environment variables, mapped network drives, display settings, installed printers which are subjected to the current user are being stored in this registry hive.
HKEY_LOCAL_MACHINE
It is also known as HKLM. It holds the major configuration information of the software and Windows operating system. It also contains information about the currently detected hardware and device drivers.
From Windows Vista to the latest version of Windows i.e Windows 10, information about the computer's boot configuration is being stored in the HKLM.
HKEY_USERS
It is also known as HKU. It holds the user-specific configuration information for all the active users. The active user here means the user who has logged in currently, also, any other users who have already logged in and switched users.
Details of the users are stored under the security identifier of each user under the HKEY_USERS root. The registry key and value of the users are stored under the corresponding security identifier. The details such as mapped network drive, environment variables, installed printers, are being stored in this registry once the user logs in for the first time.
HKEY_CURRENT_CONFIG
It is also known as HKCC. Unlike other registries, it doesn't hold any specific key and value under its hood. Instead, it acts as a shortcut point to access key and value that are related to keep information about the hardware profile of the current user.
It acts as a reference for \SYSTEM\CurrentControlSet\Hardware Profiles\Current\ registry key. HKCC exists for convenience by making it easier to edit the details in the other registries in no time.
How to access the Windows registry programmatically using C#?
So far, we have seen how to access the Windows registry and what all it contains. Let's consider a scenario where we are going to run a trial version of an application which means after a few days the application is going to expire.
The idea behind is to create a registry for an application programmatically which will hold the first use date of the application and whenever the user uses the application, it is going to validate whether the application has expired with the registry values.
Step 1
To achieve this, we are going to create a simple console application using .net framework and C# as the programming language. Open the visual studio with Administrator privileges as this solution is going to alter things in the Windows registry.
Step 2
Once the application is created, the next step is to open the program.cs file and write the code to create the registry if it is not present. If the registry key is already present then print the value that is present in the registry.
For this, we are going to use the HKEY_LOCAL_MACHINE Windows registry. And will be working under the software subkey that is present under the HKLM. Under the ‘software’ subkey, the idea is to create a subkey - ‘PARTECHApplication’ which will hold a subkey - ‘Configuration’ and its value.
Step 3
After creating the code, when we run it for the first time, the subkey will not be present. So, the code will create the subkey mentioned in step 2 and it will be saved in the registry.
Step 4
When the same code is run again, it is going to return the text that says ‘Registry already created with value’ followed by the value of it. Then, it is going to validate if the registry value date added with 30 days is still less than the value of the current date. By this, the validation of the trial can be done through the C# application.
Wrapping Up
Windows registry is a database of settings, configuration of software, hardware, and users. It is one of the safest ways to retrieve the stored configuration of the applications and it acts as a single place to store and use the essential settings and configuration of the application. So go ahead and try to access the Windows registry today.