467,212 Members | 1,064 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,212 developers. It's quick & easy.

Registry reading error

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
Nov 20 '05 #1
  • viewed: 1488
Share:
1 Reply
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

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by René Beumer | last post: by
2 posts views Thread by Joao Santa Barbara | last post: by
3 posts views Thread by Mike Malter | last post: by
6 posts views Thread by stephen coleman | last post: by
3 posts views Thread by Aussie Rules | last post: by
reply views Thread by tmsprowl@bsu.edu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.