Working with Windows Registry in VB.NET – Part 3: Reading from the Registry

Welcome to the third part!

In the previous part, part 2, we’ve learned how to write stuff to the registry. In this part you’ll learn how to read from it.


Proceed to summary of this post

Again, we’ll need to import the correct namespace and define our variable oRegKey:

'Import correct namespace
Imports Microsoft.Win32

'Define RegistryKey within our procedure or function
Dim oRegKey as RegistryKey

After that we need to point our regkey in the direction of our settings:

'Set working subkey
oRegKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\MyApp")

Note that I left the “True” out, we’re only reading stuff in this part anyway.

Once we’ve done that, we are going to fetch our settings:

'Declare variables
Dim sVersion, sAuthor, sSerial As String

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

'Close the RegistryKey
oRegKey.Close()

There you go! You now have your settings that you previously stored in the Registry back in your application for further use!


POST SUMMARY

'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

– Continue to Part 4

Leave a Reply

Your email address will not be published.

 

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