VB.NET Registry Operations

The Registry is a fundamental component of the Microsoft Windows Operating System, serving as a centralized hierarchical database. Its purpose is to store critical information required for configuring the system to meet the needs of users, applications, and hardware devices.

The Registry is organized into logical sections known as Hives. These Hives categorize and structure the stored information, allowing for efficient retrieval and management. Each Hive represents a distinct area of system configuration, such as user preferences, application settings, or hardware configurations.

The following are the predefined keys that are used by the system.

  1. HKEY_CURRENT_USER
  2. HKEY_USERS
  3. HKEY_LOCAL_MACHINE
  4. HKEY_CLASSES_ROOT
  5. HKEY_CURRENT_CONFIG

Each key has many subkeys and may have a value.

When programming in VB.NET, developers have the option to access the Registry using either the built-in functions provided by VB.NET or the dedicated registry classes available within the .NET Framework. These classes within the .NET Framework offer a more comprehensive and streamlined approach to interacting with the Registry, providing a range of functionalities for reading, writing, and modifying Registry entries.

Registry entries, within the Registry's hierarchical structure, consist of two main components: the value name and the associated value. The value name serves as the identifier or key for a particular data entry, while the value itself represents the actual data stored within that entry. This structure allows developers to retrieve and manipulate specific data within the Registry based on its corresponding value name.

Creating a Registry Entry

rKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True) rKey.CreateSubKey("AppReg")

Above code shows how to create a subkey under HKLM\Software called AppReg.

Writing values to Registry

rKey.SetValue("AppName", "RegApp") rKey.SetValue("Version", 1.1)

Above code shows how to set values to registry entry AppReg.

The following VB.NET program shows how to create a registry entry , set values to registry , retrieve values from registry and delete keys on Registry.Drag and drop four buttons on the form control and copy and paste the following source code.

Full Source VB.NET
Imports Microsoft.Win32 Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim rKey As RegistryKey rKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True) rKey.CreateSubKey("AppReg") rKey.Close() MsgBox("AppReg created !") End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim rKey As RegistryKey rKey = Registry.LocalMachine.OpenSubKey("Software\AppReg", True) rKey.SetValue("AppName", "RegApp") rKey.SetValue("Version", 1.1) rKey.Close() MsgBox("Appname , version created") End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim rKey As RegistryKey Dim ver As Decimal Dim app As String rKey = Registry.LocalMachine.OpenSubKey("Software\AppReg", True) app = rKey.GetValue("AppName") ver = rKey.GetValue("Version") rKey.Close() MsgBox("App Name " & app & " Version " & ver.ToString) End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim rKey As RegistryKey rKey = Registry.LocalMachine.OpenSubKey("Software", True) rKey.DeleteSubKey("AppReg", True) rKey.Close() MsgBox("AppReg deleted") End Sub End Class

Conclusion

By using the capabilities of VB.NET or the registry classes in the .NET Framework, developers can effectively interact with the Registry, accessing and modifying configuration settings, user preferences, and other vital system information. Understanding the structure and components of Registry entries enables developers to access and utilize the necessary data for their applications.