Checking a key in the Registry?

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
 
K

Ken Tucker [MVP]

Hi,

regValue = "Software\MyApp"



Ken

-----------------------------

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
 
H

Herfried K. Wagner [MVP]

regValue = "Software\\MyApp"

[...] keyValue = "Software\\MyApp\\UserAccepted\\Miss Piggy"

Replace the double backslashes ("\\") with single backslashes ("\"). I
assume that the code was based on a sample written in C# where backslashes
must be escaped if the string literal is not a verbatim ('@"bla\bla"')
string literal.
 
G

gsb58

Yes Indeed!

Why did I forget that I moved from C# to VB.NET?

Happy Weekend!

Me.Name
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top