Link to home
Start Free TrialLog in
Avatar of Ball_Group
Ball_Group

asked on

Reading registry value with C#

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?
Avatar of anjos
anjos

try changing
"SOFTWARE\MySoftware"
to
"SOFTWARE\\MySoftware"
hi,
try this: RegistryKey OurKey = Registry.CurrentUser;
Avatar of Ball_Group

ASKER

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 :(
ASKER CERTIFIED SOLUTION
Avatar of dexterrajesh
dexterrajesh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

dexterrajesh:
I get the same exception again when using Registry.LocalMachine.
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.
I ended up using CurrentUser instead of LocalMachine