.

Tuesday, June 17, 2008

Manipulating and Editing Registry with .NET

Author : Bhavnesh
Language : VB.NET,C#
Platform : .NET
Technology : Visual Studio

********************************************

Introduction

The Registry has a hierarchical structure much like that of the file system. The usual way of viewing and editing the registry key is by using the tools called regedit.exe and you can open it by typing regedit.exe in the run command. Quite a lot of times, you also need to write and edit the registry keys on your code or in ASP.NET and you probably haven't done it before. In this article I will teach you how to do it using .NET classes. Microsoft has included all the library that can be used to modifying the registry key inside the Microsoft.Win32 namespace.
The class is called Registry and RegistryKey. Please note that you can also accessing your registry key in your ASP.NET application, but provided that the user running the ASP.NET website has access to modify or editing your registry. The code provided below will works fine on either ASP.NET site or Windows apps.

RegistryKey instance represents a registry key. This class implements methods to browse child keys, create new keys , read or modifying the values in the key, editing the security of the registry key.

Registry class by contrast is a class that provides a static method for accessing the root keys in the registry and return a RegistryKey object. Basically there are seven of the static methods available for the RegistryKey which are called ClassesRoot, CurrentConfig, CurrentConfig, CurrentUser, DynData, LocalMachine, PerformanceData and Users.

Main

Let's start do some code on how to read the registry key.
Example if you like to read the registry key on the HKEY_LOCAL_MACHINE, then you need to write the following code.

    RegistryKey hklm = Registry.LocalMachine;
RegistryKey hkSoftware = hklm.OpenSubKey("Software");
RegistryKey hkMicrosoft = hkSoftware.OpenSubKey("Microsoft");

Or you can also use a shortcut way to access the registry key by just accessing it like XML path. Imagine if you are trying to access the key that located 10 level from the top. You don't have to write up to 10 line of code :P
    RegistryKey hkSoftware = Registry.LocalMachine.OpenSubKey("Software\\Microsoft");

A registry key accessed in this way will give you read-only access. If you want to be able to write to the key, you need to use another override to OpenSubKey which takes a second parameter of type bool.

If you are experiencing access denied when writing a key or value in the registry make sure that you set the second parameter of OpenSubKey to true.

The OpenSubKey() method is the one that you will call if you are expecting the key to be present. If the key is not exists, then it will return a null reference.

Create New Registry Key

There is a method called CreateSubKey() if you like to add new key to the registry and SetValue method if you like to write a value for the key.
Below is the code snippet
    RegistryKey key = Registry.LocalMachine.OpenSubKey("Software", true);
// Add one more sub key
RegistryKey newkey = key.CreateSubKey("WorldofASP");
// Set value of sub key
newkey.SetValue("WorldofASP.NET", "NET Developer Community");

Once you run the code, try to run regedit on your command prompt and drill down to the LocalMachine\Software key. You will notice that the key called Worldofasp.net has been added

The way that CreateSubKey() works is quite interesting. It will create the key if it doesn't already exist, but if it does already exist, then it will quietly return a RegistryKey instance that represents the existing key. The reason for the method behaving in this manner has to do with how you will normally use the registry. The registry, on the whole contains long term data such as configuration information for Windows and for various applications. It is not very common that you find yourself in a situation where you need explicitly create a key.

Get Value of the Certain RegistryKey
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Setup", false);   
MessageBox.Show(key.GetValue("BootDir").ToString());

Set Value of Certain RegistryKey
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\WorldofASP", true);
key.SetValue("Data", "C:\\Database\\World.mdf",RegistryValueKind.String);
key.SetValue("Log", "C:\\Database\\Log.txt",RegistryValueKind.String);

From the code above, you can see that it is so easy to Get and Set value for certain key. For setting the value of certain RegistryKey, you need to provide the name and the value type of the registry key.
Currently there are five type of Registry value you can provide, and you can set the registry value type by using the RegistryValueKind Enum.

Valid Registry Value Type.

1. Binary --> RegistryValueKind.Binary
2. DWORD --> RegistryValueKind.Binary
3. Expand String --> RegistryValueKind.ExpandString
4. Multi String--> RegistryValueKind.MultiString
5. Qword --> RegistryValueKind.QWord
6. String --> RegistryValueKind.String

No comments:

.