Hi,
RegistryKey.OpenSubKey returns a registry key. Here is a quick
example on how to read a key.
Dim MyReg As RegistryKey = Registry.LocalMachine
Dim MyRegKey As RegistryKey
Dim MyVal As String
MyRegKey = MyReg.OpenSubKey("Software\Microsoft\Windows NT\currentVersion")
MyVal = MyRegKey.GetValue("ProductID")
MyRegKey.Close()
Ken
--------------------------------
"Peter John" <p.*****@computer.org> wrote in message
news:51**************************@posting.google.c om...
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