C# Reading Registry Key/Value, Key is Always NULL

2.5k views Asked by At

I saw several posts on how to read registry key value on here and I think I am doing it all right but the key I read in my case is always null for some reason.

In HKLM\SOFTWARE, I created key MyCompany and then within that key, I created another key MyApp like: HKLM\SOFTWARE\MyCompany\MyApp

In this key, I added a string string value "MySetting"

I am trying to read that value using following code:

using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MyCompany\MyApp", false))
{
    string spaUrl = (String)key.GetValue("MySetting");
}

but the key is always null even though I have these keys and value set at the location above. Any idea what I am doing wrong?

I get

System.NullReferenceException was unhandled exception because key is always null.

SOLUTION

Thanks to Luke Merrett answer below, I modify the location of my keys to be in HKLM\SOFTWARE\WOW6432Node and that worked. Thanks Luke

3

There are 3 answers

2
Luke Merrett On BEST ANSWER

As Steve indicated, it may be a x86 vs x64 issue. I reproduced your code locally and when running under x86 my key was always null. Changing it to x64 allowed me to access the key.

You can change the target under Project Properties -> Build here:

enter image description here

There's some more detail on this here if you explicitly need an x86 key. Alternatively you can run %systemroot%\syswow64\regedit to add and edit x86 keys.

For reference; this works both as Admin and running as a standard user

0
Dominic Jonas On

Based on Ross Gressick answer, it is better to check if you are running a 64bit or 32bit application, than just checking if you are running on a 64bit os.

Side note: If you are using wix to set your registry keys, you need the following snippet to get the right location.

using (RegistryKey localMachine = Environment.Is64BitProcess
    ? RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
    : Registry.LocalMachine)
{
    using (var key = localMachine.OpenSubKey("SOFTWARE\\MyCompany\\MyApp"))
    {
        if (key != null)
        {
            string project = (string) key.GetValue("PROJECT");
            if (!string.IsNullOrEmpty(project))
            {
                if (project.Contains("000984"))
                {
                    // do some project specific things here
                }
                else if(project.Contains("001065"))
                {
                    // do some project specific things here
                }
            }   
        }
    }
}
0
Ross Gressick On

I feel this article answers your question the best. Reading 64bit Registry from a 32bit application

I had an issue where I had to run the application under 32bit, not Any CPU. Because of that, I kept getting NULL because it couldn't find the path. I created an IF/THEN wrapper to allow it to determine which key to use.

RegistryKey localMachine;
if (Directory.Exists("C:\\Windows\\SysWOW64"))
   { localMachine = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64); }
else { localMachine = Registry.LocalMachine; }

string productsRoot = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products";
RegistryKey products = localMachine.OpenSubKey(productsRoot);
string[] productFolders = products.GetSubKeyNames();

This solved my problem and allowed for the flexibility. IF I was just running it under "Any CPU" it wasn't an issue. But for this program, I had to specific which CPU I was operating under because a component of it required to be ran under 32bit mode only.