Functions RegRead and RegWrite - December 1997 - May 2002 - July 2006

In 1995 I wrote functions RegRead and RegWrite to read and write to the system registry. In keeping with tonight’s VB SIG meeting topic, I’m making that code available to SIG members as the tip-of-the-month.

2001/06/03 - I added support that allows you to write to alternate keys. The prior version supported reading from alternate keys, but not writing.  For example you can now read and write to:


2002/05/22 - I rewrote the functions to support VB.Net. Also added alternate key support to the demo. See the sample screen below.

2003/05/05 - The VB.Net RegWrite function was changed to return a Boolean, not an integer. Needed if 'Option Strict' is used in the calling module.

2006/07/29 - The VB.Net version is converted to VB 2005. Otherwise it is unchanged.

Demo Program RegRead

I wrote a simple VB5 demo program to read and write to the registry using functions RegRead and RegWrite. The demo program accepts a key, parameter name and optionally a parameter value.

RegRead VB 6
VB 5/6 Version

RegRead VB.Net
VB.Net Version

RegRead Instructions

You can change the key, name or value to demonstrate reading and writing to the registry. You can delete an entry in the registry by passing a ‘blank’ parameter value.

Sample Function Call

Private Sub Command1_Click(Index As Integer)
    Select Case Index
        Case 0 'regread
            If DoReg(Me, False) Then
                Text1(3).Text = " Value successfully returned."
                Text1(3).Text = " No value returned."
            End If
        Case 1 'regwrite
            If DoReg(Me, True) Then
                Text1(3).Text = " Value successfully written."
                Text1(3).Text = " Value not written, RegWrite returned False."
            End If
    End Select
End Sub

Private Function DoReg(frm As Form, bWrite As Boolean) As Boolean
    Dim sKey As String
    Dim sName As String
    Dim sValue As String
    sKey = Trim$(Text1(0).Text)
    sName = Trim$(Text1(1).Text)
    If bWrite Then 'writing?
        sValue = Trim$(Text1(2).Text)
        If RegWrite(sKey, sName, sValue) Then
            DoReg = True 'success
        End If
        Text1(2).Text = "" 'clear it
        sValue = RegRead(sKey, sName)
        If sValue <> "" Then
            Text1(2).Text = sValue
            DoReg = True 'success
        End If
    End If
End Function

Sample Registry Entries Created Using the Demo

VB 5/6 Version

VB.Net Version

Download VB Code

Click here to download the VB Source Code [6k]. The demo program is written in VB5, the functions will work with VB4 32-bit, VB5 and VB6.

Click here to download the VB.Net Source Code [11k]. Click here to download the VB 2005 Source Code [11k].

Previous Tip    Tip Index    Next Tip


Updated 2006/07/29