1

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

6
  • Do you have permission to read the registry key? Commented Jan 9, 2017 at 23:03
  • I dont know. How do I figure that out and get the permission? Commented Jan 9, 2017 at 23:03
  • A simple way to determine if this is the problem would be to run your app as Administrator. If running it that way succeeds the call then most likely the problem is permissions. In that case you need to make sure the principal the app is running as has permissions to read the specific Registry key Commented Jan 9, 2017 at 23:06
  • I am running app in debug mode from VS started as Admin Commented Jan 9, 2017 at 23:07
  • 1
    Is your app a 32bit or 64bit? Commented Jan 9, 2017 at 23:12

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

Platform target in my case is AnyCPU
Thanks Luke. I moved my registry key under HKLM\SOFTWARE\WOW6432Node\MyCompany\MyApp and that worked. I think your answer lead me to the solution. Thank you~
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.