Access Windows Registry in VB.NET

This article describe how you can work with Windows registry in VB.NET.
  • 1086

The registry has been used for storing configuration information like database connection strings, profiles etc. The reason behind registry usage is 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.

You can access the registry by using the functions which is provided by VB.NET, but in some situation you have to use another option to access the registry which is registry classes of the .NET Framework. As we use the registry to see the information about operating system and the application which is hosted by the machine, So for the security purpose you must closely scrutinize code that accesses the registry to prevent from threat to the security of the machine.

The Registry entries contain two parts: the value name and the value.

Access the Registry with VB.NET function

You can access the registry by using the following four functions, "But to use them, you must have Read and Write permission from the RegistryPermissionAccess enumeration".

  • DeleteSetting

    It deletes the key setting from an application's entry in the registry.
     
  • GetSetting

    It returns a key setting value from an application's entry in the registry.
     
  • GetAllSettings

    It returns a list of keys setting and their values from an application's entry in the registry.
     
  • SaveSetting

    It creates and saves an application entry in the registry.

Syntex Example of accessing the registry with VB.NET function

    Module
Module1
        Sub Main()
            ' Create the first key.
            SaveSetting("TestApp", "Startup", "FirstKey", "First")
            ' Create the first subkey.
            SaveSetting("TestApp", "FirstKey", "FirstSubKey", "FirstSub")
            'Create the second subkey.
            SaveSetting("TestApp", "FirstKey", "SecondSubKey", "SecondSub")
            Try
                ' Write the first key's value.
                Console.WriteLine((GetSetting("TestApp", "Startup", "FirstKey")))
                ' Write the first key as well as its two subkeys.
                Console.WriteLine(GetAllSettings("TestApp", "Startup"))
            Catch e As ArgumentException
            Catch e As Exception
                Console
.WriteLine(e.GetType.ToString)
            Finally
            End
Try
            DeleteSetting("TestApp", "FirstKey", "SecondSubKey")
            Try
                Console.WriteLine(GetSetting("TestApp", "Startup", "FirstKey"))
                Console.WriteLine(GetAllSettings("TestApp", "Startup"))
            Catch e As ArgumentException
            Catch e As Exception
                Console
.WriteLine(e.GetType.ToString)
            Finally
            End
Try
            Console.ReadLine
        End Sub
    End
Module

Access the Registry with .NET Framework

Just because of limited access of the GetSetting and SaveSetting function you have to use the Registry and RegistryKey classes in the namespace of the .NET framework.

You can access the subkeys and their values by using this following seven keys of the registry class.

  • ClassesRoot

    It describe the type and properties of type of the documents
     
  • CurrentConfig

    Information about the hardware configuration.
     
  • CurrentUser

    It store information about the current user preferences.
     
  • DynData

    It store dynamic registry data.
     
  • LocalMachine

    It hold the configuration data for the local machine.
     
  • PerformanceData

    Contains performance information for software components.
     
  • Users

    Contains information about the default user preferences.

Syntex Examples of accessing the registry with .NET Framework

It shows how to read a DWORD value from HKEY_CURRENT_USER

    Imports Microsoft.Win32
    Module Module1
        Sub Main()
            Dim regVersion As RegistryKey
            Dim keyValue As String
            keyValue = Software\\Microsoft\\TestApp\\1.0
            regVersion = Registry.CurrentUser.OpenSubKey(keyValue,
False)
            Dim intVersion As Integer = 0
            If (Not regVersion Is Nothing) Then
                intVersion = regVersion.GetValue("Version", 0)
                regVersion.Close()
            End If
        End
Sub
    End
Module

It reads, increments, and then writes a DWORD value to HKEY_CURRENT_USER

    Imports Microsoft.Win32
    Module Module1
        Sub Main()
            Dim regVersion As RegistryKey
            regVersion =
            Registry.CurrentUser.OpenSubKey(
"SOFTWARE\\Microsoft\\TestApp\\1.0", True)
            If regVersion Is Nothing Then
                ' Key doesn't exist; create it.
                regVersion =
                Registry.CurrentUser.CreateSubKey(
"SOFTWARE\\Microsoft\\TestApp\\1.0")
            End If

            Dim
intVersion As Integer = 0
            If (Not regVersion Is Nothing) Then
                intVersion = regVersion.GetValue("Version", 0)
                intVersion = intVersion + 1
                regVersion.SetValue(
"Version", intVersion)
                regVersion.Close()
            End If
        End
Sub
    End
Module

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.