Solved

Reading registry value with C#

Posted on 2011-09-26
8
239 Views
Last Modified: 2012-05-12
Hi,

I'm trying to read a registry value in my C# application (.NET 2.0), but i get the following error:

Object reference not set to an intance of an object.

My code:
 RegistryKey OurKey = Registry.LocalMachine;
            OurKey = OurKey.OpenSubKey(@"SOFTWARE\MySoftware", true);
            try
            {
                MessageBox.Show(OurKey.GetValue("LastUpdateDate").ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

Open in new window


Any suggestions?
0
Comment
Question by:Ball_Group
8 Comments
 
LVL 3

Expert Comment

by:anjos
ID: 36597892
try changing
"SOFTWARE\MySoftware"
to
"SOFTWARE\\MySoftware"
0
 
LVL 9

Expert Comment

by:dexterrajesh
ID: 36597930
hi,
try this: RegistryKey OurKey = Registry.CurrentUser;
0
 

Author Comment

by:Ball_Group
ID: 36597953
Anjos:
That does not make any difference.


dexterrajesh:
I don't wanna read from CurrentUser, because the registry key i want to read is in Local Machine.
But the strange thing is that it works with CurrentUser. I just created a few keys and values to test with, and it works fine.

Hmm.. Perhaps i should use CurrentUser, but its a bit problematic if another user is logged in :(
0
 
LVL 9

Accepted Solution

by:
dexterrajesh earned 500 total points
ID: 36597971
registry access depends on user roles and permission... you need to plan accordingly...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:dexterrajesh
ID: 36597984
if you still wanna use your code then it should be:

RegistryKey OurKey = Registry.LocalMachine;
            try
            {
                OurKey = OurKey.OpenSubKey(@"SOFTWARE\MySoftware", true);
                MessageBox.Show(OurKey.GetValue("LastUpdateDate").ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

Open in new window

0
 

Author Comment

by:Ball_Group
ID: 36598229
dexterrajesh:
I get the same exception again when using Registry.LocalMachine.
0
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 36598487
You can read from LocalMachine but you can't write to this location without permissions. If you want to write into LocalMachine it's usually done during installation and then your application only uses that information for reading back settings but never attempts to write. If you want to read/write/update values you use the CurrentUser as this gives permission.

If you remove the second parameter in this line

OurKey = OurKey.OpenSubKey(@"SOFTWARE\MySoftware", true); <---- You are opening the key for write access which would throw security exception.

OurKey = OurKey.OpenSubKey(@"SOFTWARE\MySoftware", false); <---- This should succeed because you have (read) permission to the entry.

If you get SecurityException then it's permission issue.
If you get NullReference Exception the key doesn't exists or you may have written into x64 registry location while running app under x32 bit which accesses the 32 bit registry on x64 bit OS.

x64 bit compilation read/writes to x64 bit registry on 64 bit OS.
x32 bit compilation read/writes to x32 bit registry on 64 bit OS.

You could use settings file instead to save LastUpdateDate information.
0
 

Author Closing Comment

by:Ball_Group
ID: 37676876
I ended up using CurrentUser instead of LocalMachine
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Check my code out(2) 2 54
Events in static methods 3 50
Runtime Error 2 29
Network monitoring with information includes process path and destination IP 2 13
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now