Registry Read and Write Using VB.Net


VB.Net Registry Operations

You can do basic Registry operations via Microsoft.Win32.Registry class.

1) Creating a Subkey


Imports Microsoft.Win32
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim regKey As RegistryKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
regKey.CreateSubKey("MyApp")
regKey.Close()
End Sub
End Class


The code sample above creates a subkey called MyApp under HKLM\Software. Note that the second parameter True is used to indicate whether the key is writable. You can set it to False if you are just reading data from the registry.

2) Reading and writing values


Imports Microsoft.Win32
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim regKey As RegistryKey
Dim ver As Decimal
regKey = Registry.LocalMachine.OpenSubKey("Software\MyApp", True)
regKey.SetValue("AppName", "MyRegApp")
ver = regKey.GetValue("Version", 0.0)
If ver < 1.1 Then
regKey.SetValue("Version", 1.1)
End If
regKey.Close()
End Sub
End Class


Comments

No responses found. Be the first to comment...