Solved

Reading registry value with C#

Posted on 2011-09-26
8
237 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

22 Experts available now in Live!

Get 1:1 Help Now