G
gsb58
Hi!
I have a little problem with the registry function. The function is
supposed to check the registry as the main form is activated. If there
is a key it should not write to the registry, as it does.
Can anybody see what I'm doing wrong?
Private Sub WriteToRegistry()
Dim regKey As RegistryKey = Nothing
Dim regValue As String
Try
regValue = "Software\\MyApp"
regKey = Registry.CurrentUser.CreateSubKey(regValue)
regKey.SetValue("RunOnce", "1")
regKey.SetValue("Date", Now)
regKey.SetValue("CopyRight", "©GSB")
regKey.SetValue("UserAccepted", "Miss Piggy")
regKey.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Write_to_Registry",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Function CheckRegistryKey() As Boolean
Dim regVersjon As RegistryKey = Nothing
Dim keyValue As String
Dim bExists As Boolean = False
Try
keyValue = "Software\\MyApp\\UserAccepted\\Miss Piggy"
regVersjon = Registry.CurrentUser.OpenSubKey(keyValue,
False)
If (Not regVersjon Is Nothing) Then
bExists = True
ElseIf (regVersjon Is Nothing) Then
bExists = False
WriteToRegistry()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Check_Registry_Key",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Finally
If (Not regVersjon Is Nothing) Then
regVersjon.Close()
End If
End Try
Return bExists
End Function
Private Sub frmMain_Activated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
Static IsActivated As Boolean
Try
If Not IsActivated Then
IsActivated = True
CheckRegistryKey()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "MAIN_ACTIVATED",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
Hope the code is readable.
Me.Name
I have a little problem with the registry function. The function is
supposed to check the registry as the main form is activated. If there
is a key it should not write to the registry, as it does.
Can anybody see what I'm doing wrong?
Private Sub WriteToRegistry()
Dim regKey As RegistryKey = Nothing
Dim regValue As String
Try
regValue = "Software\\MyApp"
regKey = Registry.CurrentUser.CreateSubKey(regValue)
regKey.SetValue("RunOnce", "1")
regKey.SetValue("Date", Now)
regKey.SetValue("CopyRight", "©GSB")
regKey.SetValue("UserAccepted", "Miss Piggy")
regKey.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Write_to_Registry",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Function CheckRegistryKey() As Boolean
Dim regVersjon As RegistryKey = Nothing
Dim keyValue As String
Dim bExists As Boolean = False
Try
keyValue = "Software\\MyApp\\UserAccepted\\Miss Piggy"
regVersjon = Registry.CurrentUser.OpenSubKey(keyValue,
False)
If (Not regVersjon Is Nothing) Then
bExists = True
ElseIf (regVersjon Is Nothing) Then
bExists = False
WriteToRegistry()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Check_Registry_Key",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Finally
If (Not regVersjon Is Nothing) Then
regVersjon.Close()
End If
End Try
Return bExists
End Function
Private Sub frmMain_Activated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
Static IsActivated As Boolean
Try
If Not IsActivated Then
IsActivated = True
CheckRegistryKey()
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "MAIN_ACTIVATED",
MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
Hope the code is readable.
Me.Name