|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Services
Chapters
Feature Zones
|
IntroductionDevelopers have always found the windows registry to be a suitable place for storing application specific information and configuration settings. Traditionally, the registry has been used for storing configuration information like database connection strings, profiles etc. The popularity of the registry can be attributed to the fact that registry access is faster than file access and also because it is a very secure system-wide data repository. Moreover, configuration files like INI files had their own limitations. In this article I shall discuss some basics of Windows registry and then explain how basic registry operations can be done in VB.NET Basics of Windows RegistryThe registry is organized as a hierarchical structure. It has basically five predefined keys under which all data is added or accessed. These keys cannot be renamed or deleted .Given below is a table containing a brief description about them.
Each key has many subkeys and may have a value. Given below is a snapshot of the registry as seen through the registry editor (Regedit.exe), which comes along with windows.
Fig 1: Registry Structure In the snapshot shown above, each node under My Computer is a key. For example , HKEY_CURRENT_CONFIG is a key which has two subkeys: Software and System. Fonts is a subkey under software and has values. Each Value contains a name and its associated data. Each value needs to be associated with a particular data type. Given below is a table containing the important data types
Referring back to figure 1, there is a value called LogPixels which has data of type REG_DWORD and value 96. Working with Microsoft.Win32 NamespaceThe operations on the registry in .NET can be done using two classes of the Microsoft.Win32 Namespace: Registry class and the RegistryKey class.The Registry class provides base registry keys as shared public (read-only) methods:
Each of the public methods shown above provides an object of the RegistryKey class whose methods can be used to access subkeys under the corresponding keys. The important members of the RegistryKey class are enlisted below Public Properties
Public Methods
VB.NET and RegistryI shall now come to the practical aspect and see how the registry can be manipulated using VB.NET code. I have considered three basic operations: Creating a subkey, adding values and deleting a subkey Creating a SubkeyDim regKey As RegistryKey regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True) regKey.CreateSubKey("MyApp") regKey.Close()In the code snippet shown above, I have created a subkey under HKLM\Software called MyApp. Note that I passed True as the second parameter to the OpenSubKey method. This boolean value is to indicate whether the key is writable or not. For instance, you can set it to false if you are just reading data from the registry. Reading and writing valuesDim regKey As RegistryKey Dim ver As Decimal regKey = Registry.LocalMachine.OpenSubKey("Software\MyApp", True) regKey.SetValue("AppName", "MyRegApp") ver = regKey.GetValue("Version", 0.0) If ver < 1.1 Then regKey.SetValue("Version", 1.1) End If regKey.Close() In the code snippet shown above, I am creating two values AppName and Version. I am also setting the values to MyRegApp and 1.1 respectively. Note: If you recollect that in the previous sections I had mentioned about data types for registry values like REG_SZ. But nowhere in the above code we mentioned about the data type. This is because .NET runtime interprets the type itself based on what is passed as value and we do not need to pass it explicitly. Deleting a SubkeyDim regKey As RegistryKey regKey = Registry.LocalMachine.OpenSubKey("Software", True) regKey.DeleteSubKey("MyApp", True) regKey.Close() In the code snippet shown above, I am deleting the subkey MyApp and all its values. Note that in the call to DeleteSubkey, I have passed a second Boolean argument of True. This means that an exception is thrown when the key to be deleted is not found Important
ConclusionThis article hopefully has made it easier for beginners to understand the registry and to do simple operations with it. A lot more operations are possible with the Registry and RegistryKey classes but for the sake of simplicity, I have omitted all those here.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||