locked
How do I read/write to the Registry in VB.NET?

Answers

  • 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
    

     

    The code sample creates two values AppName and Version, and sets the values to MyRegApp and 1.1 respectively.

     

    3)   Deleting 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.DeleteSubKey("MyApp", True)
            regKey.Close()
        End Sub
    End Class
    

     

    The code sample deletes the subkey MyApp and all its values.

     

    Tutorial: Windows Registry Basics and Working with Windows Registry using VB.NET

    http://www.codeproject.com/KB/vb/registry_with_vb.aspx

     

    Also you can do basic Registry operations via My.Computer.Registry in VB.NET.

    Tutorial: How to use (add, read, change, delete) registry key or value using VB.NET

    http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/5b22e94c-37a9-4be5-ad55-3d9229220194/

     

    For more FAQ about Visual Basic Express, please see Visual Basic Express FAQ

     

     

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, April 09, 2009 2:06 PM