If the specified key doesn't exist,
the OpenSubKey
method[
^] will return
Nothing
. Your code then attempts to call
the GetValue
method[
^] on the returned value, which results in a
NullReferenceException
.
If you use
the CreateSubKey
method[
^] instead, it will create the key if it doesn't exist:
Using key As RegistryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\ABCD\DB", True)
If key Is Nothing Then
Throw New InvalidOperationException("Failed to create the registry key.")
End If
If key.GetValue(RegName) Is Nothing Then
key.SetValue(RegName, RegVal)
End If
End Using