Simplistic Registry Access - Any Issues With It

Sprint

Well-known member
Joined
Feb 3, 2006
Messages
58
Location
Ohio
Programming Experience
5-10
I have been using what I think is a simplistic and easy way of reading and writing to/from the registry but whenever I look at code I keep seeing links to DLL's and constant decorations and lines and lines of code. Is there any reason why I shouldn't use the following:

VB.NET:
    Friend Function ReadCURKString(ByVal Subkey As String, ByVal KeyToRead As String, ByVal DefaultString As String) As String
        Dim rk As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(Subkey, True)
        Try
            ReadCURKString = CType(rk.GetValue(KeyToRead), String)
            rk.Close()
        Catch ex As Exception
            ReadCURKString = DefaultString
            Debug.WriteLine(ex.Message)
        End Try
    End Function

    Friend Sub WriteCURKValue(ByVal SubKey As String, ByVal KeyName As String, ByVal KeyValue As String, ByVal KeyType As Microsoft.Win32.RegistryValueKind)
        Dim rk As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(SubKey)
        Try
            rk.SetValue(KeyName, KeyValue, KeyType)
            rk.Close()
        Catch ex As Security.SecurityException
            MsgBox("Unable to write to the registry!  You may not have high enough security for registery access." & vbLf & "Subkey: HKey_Current_User" & SubKey & vbLf & "Key Name: " & KeyName & vbLf & "Value: " & KeyValue, MsgBoxStyle.OkOnly, "Registry Error")
            Debug.WriteLine(ex.Message)
        End Try
    End Sub

    Friend Sub DeleteCURKValue(ByVal SubKey As String, ByVal KeyName As String)
        Dim rk As Microsoft.Win32.RegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(SubKey)
        Try
            rk.DeleteValue(KeyName)
            rk.Close()
        Catch ex As Security.SecurityException
            MsgBox("Unable to delete from the registry!  You may not have high enough security for registery access." & vbLf & "Subkey: HKey_Current_User" & SubKey & vbLf & "Key Name: " & KeyName, MsgBoxStyle.OkOnly, "Registry Error")
            Debug.WriteLine(ex.Message)
        End Try
    End Sub

Usage example:
VB.NET:
    Private Sub UpdateRunOnStartup()
        If RunOnStartupBarCheckItem.Checked Then
            WriteCURKValue("Software\Microsoft\Windows\CurrentVersion\Run", "MyProgramsStartup", My.Application.Info.DirectoryPath.ToString & "\" & My.Application.Info.AssemblyName & ".exe", Microsoft.Win32.RegistryValueKind.String)
        Else
            If ReadCURKString("Software\Microsoft\Windows\CurrentVersion\Run", "MyProgramsStartup", "") <> Nothing Then
                DeleteCURKValue("Software\Microsoft\Windows\CurrentVersion\Run", "MyProgramsStartup")
            End If
        End If
    End Sub

-Allan
 
No, I just always see examples online that use stuff like this:
VB.NET:
    <DllImport("advapi32.dll", EntryPoint:="RegDeleteValue")> Public Shared Function RegDeleteValueA(ByVal hKey As Integer, ByVal lpValueName As String) As Integer
    End Function

and then call stuff through it and I'm not sure why when you can just use what's built in.
 
Back
Top