Solved

Reading registry value with C#

Posted on 2011-09-26
8
247 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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Include MS Database in Winforms Project using Intellilock 5 31
C# DataTable (in memory) vs SQL Lookups 4 67
c# ftp code 3 53
Run software updates from the website 6 46
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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