P
Peter John
I am using the following code to read from and write to the registry.
The writing works fine but reading always fails. Can anyone suggest
what is going wrong?
Imports Microsoft.Win32
Public Class clsRegistry
Private Const conRegKey As String =
"Software\MyCompany\MyApplication"
Public Sub New()
Dim rgKey As RegistryKey = Registry.CurrentUser
Dim strStr As String
rgKey.OpenSubKey(conRegKey)
strStr = rgKey.Name
If strStr = "HKEY_CURRENT_USER" Then
'create the subkey
rgKey.CreateSubKey(conRegKey)
End If
rgKey.Close()
End Sub
Public Function GetProject() As String
Dim rgKey As RegistryKey = Registry.CurrentUser
Dim str As String
rgKey.OpenSubKey(conRegKey)
str = rgKey.GetValue("MyKeyVal", "fail")
'always returns "fail"
GetProject = str
rgKey.Close()
End Function
Public Function SetProject(ByVal str As String) As Boolean
Dim rgKey As RegistryKey = Registry.CurrentUser
rgKey = rgKey.OpenSubKey(conRegKey, True)
rgKey.SetValue("MyKeyVal", str)
rgKey.Close()
SetProject = True
End Function
End Class
The writing works fine but reading always fails. Can anyone suggest
what is going wrong?
Imports Microsoft.Win32
Public Class clsRegistry
Private Const conRegKey As String =
"Software\MyCompany\MyApplication"
Public Sub New()
Dim rgKey As RegistryKey = Registry.CurrentUser
Dim strStr As String
rgKey.OpenSubKey(conRegKey)
strStr = rgKey.Name
If strStr = "HKEY_CURRENT_USER" Then
'create the subkey
rgKey.CreateSubKey(conRegKey)
End If
rgKey.Close()
End Sub
Public Function GetProject() As String
Dim rgKey As RegistryKey = Registry.CurrentUser
Dim str As String
rgKey.OpenSubKey(conRegKey)
str = rgKey.GetValue("MyKeyVal", "fail")
'always returns "fail"
GetProject = str
rgKey.Close()
End Function
Public Function SetProject(ByVal str As String) As Boolean
Dim rgKey As RegistryKey = Registry.CurrentUser
rgKey = rgKey.OpenSubKey(conRegKey, True)
rgKey.SetValue("MyKeyVal", str)
rgKey.Close()
SetProject = True
End Function
End Class