By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,866 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,866 IT Pros & Developers. It's quick & easy.

Registry reading error

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.