Working with Windows Registry in VB.NET – Part 5: Everything in a nutshell

Welcome to the fifth and final part!

In this post I’ll provide a complete overview of all the actions discussed in the previous 3 parts for those who don’t feel like reading through the entire explanation on each post 😛

Let’s get started shall we? 🙂

Table of contents


Part 2 – Writing to the Registry

'Import correct namespace
Imports Microsoft.Win32

'Procedure to store stuff in the Registry
Public Sub fnWrite()
	'Declare variables
	Dim oRegKey As RegistryKey

	'Set the subkey to work with
	oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)

	'Create a new subkey
	oRegKey.CreateSubKey("MyApp")

	'Set the subkey to work with
	oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MyApp", True)

	'Create new values
	oRegKey.SetValue("Version", "1.0.0.0")
	oRegKey.SetValue("Author", "Dirk Schuermans")
	oRegKey.SetValue("Serial", "A1B2C3D4E5F6G7H8I9")

	'Close the key
	oRegKey.Close()
End Sub


Part 3 – Reading from the Registry

'Import correct namespace
Imports Microsoft.Win32

'Procedure to read stuff from the Registry
Public Sub fnRead()
	'Declare variables
	Dim oRegKey as RegistryKey
	Dim sVersion, sAuthor, sSerial As String

	'Set the SubKey to work with
	oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MyApp")

	'Retrieve settings from Registry
	sVersion = oRegKey.GetValue("Version")
	sAuthor = oRegKey.GetValue("Author")
	sSerial = oRegKey.GetValue("Serial")
End Sub


Part 4 – Deleting from the Registry

'Import correct namespace
Imports Microsoft.Win32

Public Sub fnDelete()
	'Declare variables
	Dim oRegKey As RegistryKey

	'Define subkey to work with
	oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)

	'Delete our subkey
	oRegKey.DeleteSubKeyTree("MyApp")

	'Close the RegistryKey
	oRegKey.Close()
End Sub


Everything put together

This is a code snippet you can basicly copy / paste straight into your Visual Studio if you meet the following requirements:

  1. You have a form with 3 buttons called Form1
    1. btnWrite
    2. btnRead
    3. btnDelete
'Import correct namespace
Imports Microsoft.Win32
Public Class Form1
    'Declare variables, scope: global
    Dim oRegKey As RegistryKey

    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
        'Define what subkey to work with
        oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)

        'Create a new subkey
        oRegKey.CreateSubKey("MyApp")

        'Define what subkey to work with
        oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MyApp", True)

        'Insert new values
        oRegKey.SetValue("Test1", "test1")
        oRegKey.SetValue("Test2", "test2")
        oRegKey.SetValue("Test3", "test3")

        'Create a new subkey
        oRegKey.CreateSubKey("TEST2")

        'Define what subkey to work with
        oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MyApp\TEST2", True)

        'Insert new values
        oRegKey.SetValue("Test4", "test4")
        oRegKey.SetValue("Test5", "test5")
        oRegKey.SetValue("Test6", "test6")

        'Close the RegistryKey
        oRegKey.Close()

        MessageBox.Show("Registry entries have been created!", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        'Define variables
        Dim sResult As String = ""

        'Define what subkey to work with
        oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MyApp")

        'Go over each value within the subkey
        Try
            For Each sValue As String In oRegKey.GetValueNames
                'Add it to the results
                sResult += sValue & " = " & oRegKey.GetValue(sValue) & vbNewLine
            Next
        Catch ex As NullReferenceException
            MessageBox.Show("It seems that the subkey SOFTWARE\MyApp does not exist. Please create it first.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)

            Exit Sub
        End Try

        'Show the results
        MessageBox.Show("HKEY_LOCAL_MACHINE\SOFTWARE\MyAPP contains the following values:" & vbNewLine & vbNewLine _
                        & sResult, "Results", MessageBoxButtons.OK, MessageBoxIcon.Information)

        'Clear the results
        sResult = ""

        'Define what subkey to work with
        oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MyApp\TEST2")

        'Go over each value within the subkey
        Try
            For Each sValue As String In oRegKey.GetValueNames
                'Add it to the results
                sResult += sValue & " = " & oRegKey.GetValue(sValue) & vbNewLine
            Next
        Catch ex As NullReferenceException
            MessageBox.Show("It seems that the subkey SOFTWARE\MyApp does not exist. Please create it first.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)

            Exit Sub
        End Try

        'Show the results
        MessageBox.Show("HKEY_LOCAL_MACHINE\SOFTWARE\MyAPP\TEST2 contains the following values:" & vbNewLine & vbNewLine _
                        & sResult, "Results", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        'Define what subkey to work with
        oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)

        'Delete the subkey
        Try
            oRegKey.DeleteSubKeyTree("MyApp")

            MessageBox.Show("Registry entries have been deleted!", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As ArgumentException
            MessageBox.Show("It seems that the subkey you're trying to delete does not exist. Please create it first", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        'Close the RegistryKey
        oRegKey.Close()
    End Sub
End Class

4 thoughts on “Working with Windows Registry in VB.NET – Part 5: Everything in a nutshell”

  1. I noticed that you are trying to write to KHLM.

    This behavior should be avoided since HKLM requires administrative rights.

    So please write to HKCU, since there you have the rights.

    And please replace vbNewLine by Environment.NewLine.

    vbNewLine is deprecated.

    1. And since when would vbNewLine be decrepated?

      Can’t find anything related to this…

      In regards to the whole HKLM thing: It’ll depend on the scope of your program wether to write to HKLM or to HKCU. This was just a HKLM example.

      1. In reply to my previous post.

        vbNewLine is always \n\r

        while Environment.NewLine is Operating System dependent. When you compile a .NET app on Linux it will result in \n, and on Mac it will result in \r.

Leave a Reply

Your email address will not be published.

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.