Advertisment

Registry in .Net

author-image
PCQ Bureau
New Update

The registry is one of the most important parts of the Windows OS, primarily because it contains volumes of information regarding the OS’s configuration and the hardware and software configuration details, amongst other things. From the point of view of a developer, access to the registry is even more important so that he can ensure that applications have access to all the system related details that they require, and also define their own behavior by storing their configuration in the registry.

Advertisment

Before .Net, access to the registry was using the Win32 Registry APIs, exported by the ADVAPI DLL. Under .Net, the same set of APIs can be used through Platform Invocation Services (PInvoke) since .Net is about working with and executing managed code, that is, code run under the supervision of CLR (Common Language Runtime), while the Registry APIs exist in the non-managed (non-.Net) world. Coding definitely becomes inconvenient and terse. So, an easier approach is to use the Microsoft.Win32 namespace from the .Net class library. 

We shall look at the Microsoft.Win32 namespace’s registry oriented classes, and how to use their methods to interact with the registry. In this process, we shall create a console .Net application, programmed using C# that will assist us in installing and uninstalling application entries in the HKEY_LOCAL_MACHINE\Software\

Microsoft\Windows\CurrentVersion\Run registry key. For those who don’t know, applications pointed to by entries in this location run automatically (something like what AUTOEXEC.BAT did in the days of DOS) when a user logs on. 

Microsoft.Win32



Microsoft.Win32 is one of the five (as of .Net Beta 2) namespaces named after the company. In addition to supporting registry access, this namespace also supports working with various system related and power related event handling for the OS. But, of course, we’ll focus on registry access.

Advertisment

To work with the registry, the namespace provides two class: Registry and RegistryKey. The Registry class is sealed in nature and thus, cannot be inherited, with its main purpose being providing access to the root registry keys:

  • HKEY_LOCAL_MACHINE
  • HKEY_CLASSES_ROOT
  • HKEY_CURRENT_USER
  • HKEY_USERS
  • HKEY_CURRENT_CONFIG
  • HKEY_DYNAMIC_DATA
  • HKEY_PERFORMANCE_DATA

Of course, not all of these keys are present on all Windows OS. The Registry class contains a static field for each of these classes, which is of the type

RegistryKey.

Advertisment

In the process of designing the application discussed earlier, we would need to get a reference to the HKEY_LOCAL_MACHINE registry key. Hence, we use the following piece of code:

RegistryKey rkHKLM =



Registry.LocalMachine;

Of course, we do supply a reference to the Microsoft.Win32 namespace before any code referencing the use of registry classes.

Advertisment

Talk to the registry



Once we get a reference to the required base registry key, we can use any one of the many methods and/or properties of the RegistryKey class to interact with the registry. You will find the code of our application in reference to which we shall proceed to understand the protocol of interacting with the registry, in main.cs on our this month’s CD.

To understand the source code, we’ll start by referencing the required name spaces. This will be followed by displaying the application name, and checking the number of command-line arguments passed to our application. These arguments specify if the application would be creating an entry for an application in the

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run registry key, or deleting its entry from there. The first parameter is “/I” to create an entry, or “/U” to remove an entry. The name of the entry is the second parameter, while the application pointed to by the entry is the third parameter. The third parameter is ignored by the application if it is deleting its entry, since we just need the entry name for the deletion.

Once all the checks have been made, we proceed to get a reference to the HKEY_LOCAL_MACHINE registry key. This reference is actually RegistryKey type, and we use its OpenSubkey method to attempt to open the Software\Microsoft\Windows\CurrentVersion\Run subkey in a try block. This method has two overloaded versions–one that opens the subkey in the read-only mode, and the second one that specifies if we wish to open it in write-access mode. We have used the second method, in which the first parameter is the subkey to be opened, and the second parameter is a boolean value, which if set to true, opens the subkey in a write-access mode. 

Advertisment

Once that is done, if an error occurs during the opening of the subkey, an exception will be thrown. We aren’t interested in the exact nature of the exception, so we have used a generic “catch-all” catch block, which displays the error message and closes the HKEY_LOCAL_MACHINE subkey using the Close method of the

RegistryKey class. 

If, however, the subkey is opened successfully, we store the reference to the same, and proceed to determine the nature of our work, that is, whether to create an entry or delete it. To create an entry in the subkey, we call the SetValue method of the RegistryKey class. The first parameter of this method is the name of the entry that will be created, and the second parameter is the value of the entry, which in this case is the path to the application to be auto-executed.

Likewise, to delete an entry, a call is made to the DeleteValue method of the RegistryKey class, which takes just one parameter: the name of the entry to be deleted. If any error occurs during the creation or the deletion of the entry, an exception will be thrown, which the code catches using the generic “catch-all” block. Appropriate messages are then displayed. 

Advertisment

Once everything is done, we close the registry keys in the reverse order in which they were opened. 

That is all there is to have a basic to-fro interaction with the registry. Of course, we haven’t covered all the methods and properties, but their usage is quite intuitive. But the point is the ease with which we could interact with the registry. If you have ever programmed for the registry using the Win32 Registry APIs, you will definitely see the difference between the approach of .Net and that of the Win32 APIs, and will agree that the .Net class has made things very simple to work with. 

Kumar Gaurav Khanna

Advertisment