views:

11

answers:

1

Dears good morning to all I'm using the following code to open and then write a subkey in the registry

Private Sub RegSubKeyCreates(ByVal sKeyPath As String)
      Dim RegKey As RegistryKey
      Dim SubKeyParam() As String = Nothing

      SubKeyParam = Split(sKeyPath, "\")
      RegKey = Registry.CurrentUser.OpenSubKey(SubKeyParam(0), True).OpenSubKey(SubKeyParam(1), True)
      RegKey.CreateSubKey(SubKeyParam(0)).CreateSubKey(SubKeyParam(1))

End Sub

When i'm trying to create the subkey i'm geting the error System.NullReferenceException Object reference not set to an instance of an object I really can't understand what is going on please is there some one to assist me on this?

+1  A: 

Thank you very much for any one trying to send an answer. But i found the solution my self. So i put it here in case that someone else want to see it. The solution is in the mentality of working with registry Hive. Needs to understand that the sub keys creates one by one, only after we have open the registry to the last existent sub key. The new sub key don't need to have any character before or after "subkey" like that please look at the code.

Private Sub RegSubKeyCreates(ByVal sKeyPath As String)
      Dim RegKey As RegistryKey
      Dim SubKeyParam() As String = Nothing
      Dim UB As Integer
      Dim Bound As Integer
      Dim KeyPath As String

      SubKeyParam = Split(sKeyPath, "\")
      UB = UBound(SubKeyParam)
      For Bound = 0 To UB - 1
           KeyPath = KeyPath + SubKeyParam(Bound) + "\"
      Next
      RegKey = Registry.CurrentUser.OpenSubKey(KeyPath, True)
      RegKey.CreateSubKey(SubKeyParam(UB))

 End Sub

And the sub which leads one this is:

Select Case RegKeyExists("Software\sKey1")
           Case True
           Case False
                RegSubKeyCreates("Software\skey1")  
                Select Case RegKeyExists("Software\sKey1\sKey2")
                     Case True
                     Case False
                          RegSubKeyCreates("Software\sKey1\sKey2")
                          Select Case RegKeyExists("Software\sKey1\sKey2\sKey3")
                               Case True
                               Case False
                                    RegSubKeyCreates("Software\sKey1\sKey2\sKey3")
                                    Select Case RegKeyExists("Software\skey1\sKey2\sKey3")
                                         Case True
                                              SetRegKeyValue("KeyName", "KeyValue", "TheTotalSubKeyPath")
                                         Case False

                                    End Select
                          End Select
Lefteris Gkinis