Solved

Reading registry value with C#

Posted on 2011-09-26
8
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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