problem save and reading from the registry

A

Aussie Rules

Hi,

I want to store some data in the registry, however I have not been able to
do this, and think my logic maybe flawed.

Firstly I try to open the registry and read in any existing values.
Dim aKey As RegistryKey
aKey = Registry.CurrentUser.OpenSubKey("software\myApplication")

I then attempt to read in any existing values
strServerName = aKey.GetValue("ServerName")
strLoginName = aKey.GetValue("LoginName")

When the first getValue is executed, i get an error 'object reference not
set to an instance of an object', which I guess is fine as this registry
entry doesn't exist yet.

If the registry entry doesn't exist (say first time the application is run),
then I capture the error on the above code and create the registry subkey=
Registry.CurrentConfig.CreateSubKey("software\myApplication") ' No error on
this line

After the user has logged in, i want to save the user id and server name to
the registry, so that next time they use the program, step 1 will populate
the login form with the last login details used.

aKey.SetValue("ServerName", frmSPlash.txtServer.Text)
aKey.SetValue("LoginName", frmSPlash.txtUserID.Text)

When the first setvalue is execute i get an error 'object reference not set
to an instance of an object'. which i would not expect as it should be just
creating the entry.

What have I done wrong here......

THanks
 
I

Izzy

Have you looked at the app.config file? This replaces storing settings
in the registry for .NET apps. If you using VS 2005 it's very simple:

'Get Value
Value = My.Settings.[SettingsName]

'Set Value
My.Settings.[SettingsName] = Value
 
P

Peter Proost

I think the problem is that you read from a different location then you
write to:

Registry.CurrentUser.OpenSubKey("software\myApplication")
Registry.CurrentConfig.CreateSubKey("software\myApplication")

Below is a working sample, hope this helps:

Greetz Peter

Dim myKey As RegistryKey
myKey = Registry.CurrentUser.OpenSubKey("Software\testNG")
If myKey Is Nothing Then
myKey = Registry.CurrentUser.OpenSubKey("Software", True)
myKey.CreateSubKey("testNG")
myKey = Registry.CurrentUser.OpenSubKey("Software\testNG", True)
myKey.SetValue("testNG", "Peter Proost")
Else
MsgBox(myKey.GetValue("testNG"))
End If
 
J

Jeffrey Tan[MSFT]

Hi Aussie,

Additional to Peter's reply:

What you got is the NullReferenceException, which normally means that aKey
is a null reference. The simplest way is set a breakpoint in aKey.SetValue
line, and view aKey variable in Watch Window in VS.net IDE debugger.

If once you find the null reference, you should check why aKey is not
assigned a real object instance.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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