SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software
ASP.NET, VB.NET, Microsoft .NET Framework, Microsoft Visual Studio, Windows Forms, Controls and more Tutorials and Articles for Programmers

How to Read and Write from Registry Key Values

If you are coding or programming in .NET environment and if you need to deal with system registry within your application, then you should use Microsoft.Win32 namespace in your .Net project. Microsoft.Win32 namespace provides the necessary classes and methods that enables us to manipulate the system registry.

The registry is the main information depot where information about the operating system and the applications running on this operating system exists. The data stored in the registry is arranged in an hierarchical format.

The RegistryKeys are the units of the hierarchical data storage in system registry. Each key may have subkeys. Keys may have multiple key-value pairs associated with them. Values associated with key can be edited, deleted or new values can be created related with that key.

If you are creating an application using .NET platform and want to store some information in the system registry you can create store your key value pairs in system registry in a format similar to "My Computer\HKEY_LOCAL_MACHINE\SOFTWARE\KODYAZ\RegistrySampleApplication" where instead of "KODYAZ" you can use your company name and you can replace "RegistrySampleApplication" with your application name.

The main classes that enables the developers to create and edit registry keys and values are "Registry" and "RegistryKey" classes.

Registry class supplies the base RegistryKeys that access values and subkeys in the registry.

RegistryKey class represents a key level node in the windows system registry.

Some of the properties of RegistryKey are :

  • Name, representing the name of the key.
  • SubKeyCount, representing the count of the subkeys for that key.
  • ValueCount, represents the count of values for that key.
  • And some of the major methods are :
  • CreateSubKey, creates a new subkey.
  • OpenSubKey, retrieves the pointed subkey.
  • GetSubKeyNames, retrieves the array of subkey names.
  • GetValue, retrieves a desired value.
  • SetValue, sets the desired value.

  • Since Registry is the base class which supplies the root key that access values and subkeys in the registry, our application should begin with the Registry value.

    Registry.LocalMachine contains information related with the local computer. These values are stored in the windows registry under HKEY_LOCAL_MACHINE.

    Registry.LocalMachine contains five subkeys. One of them is "Software" which contains configuration and installation information about installed software on that computer.

    If you want to create a registry in the system registry for "SOFTWARE\KODYAZ\RegistrySampleApplication" under "HKEY_LOCAL_MACHINE" then you should get the root registry key by the sample below,

    Dim registryKey As RegistryKey
    registryKey = Registry.LocalMachine

    Then, to create the "SOFTWARE\KODYAZ\RegistrySampleApplication" key hierarchy, use the following sample code statements

    Dim registrySubKey As RegistryKey
    registrySubKey = registryKey.CreateSubKey("SOFTWARE\KODYAZ\RegistrySampleApplication")

    If you are not sure whether the subkey exists or not, you may want to test if it is already existing in the registry by the method shown in the sample below,

    registrySubKey = registryKey.OpenSubKey("SOFTWARE\KODYAZ\RegistrySampleApplication", True)
    If registrySubKey Is Nothing Then
      registrySubKey = registryKey.CreateSubKey("SOFTWARE\KODYAZ\RegistrySampleApplication")
    End If

    Note that OpenSubKey method of the RegistryKey class takes a boolean parameter named "writable". This parameter states that the key is opened for edit. So if the key in the sample exists in the system registry, then it is opened editable.

    registrySubKey.SetValue("Version", "")

    The above sample is a simple value creating or editing which belongs to the registrySubKey in the registry.

    When you get a RegistryKey, you may want to list its sub keys and values.

    ' Lists Sub Key Names
    Dim subKeyNames As String() = registryKey.GetSubKeyNames()

    Dim subKeyName As String
    For Each subKeyName In subKeyNames
      lbLocalMachine.Items.Add(subKeyName)  ' subKeyName is the name of one of the Registy Sub Keys

    ' Lists Value Names
    Dim valueNames As String() = registryKey.GetValueNames()

    Dim valueName As String
    For Each valueName In valueNames
      lbValueName.Items.Add(valueName)  ' valueName is the name of one of the Values belonging to the Registry Key

    You will find a Visual Studio 2003 VB.NET project named "RegistrySampleApplication" in the files section which contains samples to list, create and edit Registy Keys, Values and Value Data.

    You can either download the sample VB.NET project by this link.

    Sample Registry Editing Application in VB.NET

    The following is no more valid : Sample Registry Editing Application in VB.NET

    Here is a screen shot from the sample VB.NET registry application

    sample VB.NET Registry Application

    Visual Studio

    Copyright © 2004 - 2021 Eralper YILMAZ. All rights reserved.