Resolved Registry Function

amnon

Member
Joined
Dec 18, 2022
Messages
6
Programming Experience
Beginner
Hi,

My Function Set value to the Registry, check to see if a registry SubKeys exist, if not add SubKeys.
Function works fine but I can't manage with returned boolean.


VB.NET:
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

 MessageBox.Show(AddKeySetValue(BaseKey, SubKey, Counter, KeyTable, ValueName, Value))


End Sub


    Function AddKeySetValue(BaseKey As RegistryHive, SubKey As String, Counter As Integer, KeyTable() As String, ValueName As String, Value As String) As Boolean

        Using MyBaseKey = RegistryKey.OpenBaseKey(BaseKey, RegistryView.Registry64)
            Using MySubkey = MyBaseKey.OpenSubKey(SubKey, RegistryKeyPermissionCheck.ReadWriteSubTree)

                ' ****** Check how many SubKeys need to be add, according to SubKey String ***********************
                If MySubkey Is Nothing Then
                    Dim MySubkeyString As String = SubKey.Substring(0, SubKey.LastIndexOf("\"))
                    Dim RestSubKey As String = SubKey.Substring(SubKey.LastIndexOf("\") + 1)
                    ReDim Preserve KeyTable(Counter)
                    KeyTable(Counter) = RestSubKey
                    Counter = Counter + 1
                    AddKeySetValue(BaseKey, MySubkeyString, Counter, KeyTable, ValueName, Value)
                Else
                    ' ***** If Subkey Exist Create ValueName and set Value *************
                    If KeyTable Is Nothing Then
                        Try
                            MySubkey.SetValue(ValueName, Value)
                            Return True
                        Catch ex As Exception
                            MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                            Return False
                        End Try
                    Else
                        ' ***** If Subkey does not exist, add SubKey-s according to Names in the KeyTable *************
                        For i As Integer = KeyTable.Length - 1 To 0 Step -1
                            Dim MyNewSubkey As RegistryKey = MyBaseKey.OpenSubKey(SubKey, RegistryKeyPermissionCheck.ReadWriteSubTree)
                            MyNewSubkey.CreateSubKey(KeyTable(i))
                            SubKey = SubKey & "\" & KeyTable(i)
                        Next
                        Dim MyNewNewSubKey As RegistryKey = MyBaseKey.OpenSubKey(SubKey, RegistryKeyPermissionCheck.ReadWriteSubTree)
                        Try
                            MyNewNewSubKey.SetValue(ValueName, Value)
                            Return True
                        Catch ex As Exception
                            MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                            Return False
                        End Try
                    End If
                End If

            End Using
        End Using

    End Function

Best Regards
Amnon
 
I can't manage with returned boolean.
I don't know what that means. Please provide a FULL and CLEAR e4xplanation of the problem. Explain in detail EXACTLY what you expect to happen and what actually does happen.
 
If MySubkey Is Nothing (line 17) you do a recursive call at line 23, when this completes the initial call return default value (False) always. Instead you must return the result of the recursive call, line 23 should be Return AddKeySetValue(...)
 
It doesn't seem to me that you could have debugged this code, because if you had then you would have seen that the initial call wasn't actually explicitly returning a value. The IDE almost certainly would have warned you that not all code paths return a value. You should not have ignored that warning.
 
Thank you both for your help, I'm a very beginner but thanks to this I realized that I have no idea how to debugging a program. I'm going to improve on this.
Regards
Amnon
 
Back
Top