0

My below code fails no matter if I run it as Administrator or not:

var suff = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\CCM\\LocationServices", true);
var value = suff.GetValue("DnsSuffix").ToString();

I get this error message which I can't decode:

An unhandled exception of type 'System.NullReferenceException' occurred in MyApp.exe Additional information: Object reference not set to an instance of an object.

I know for a fact that the value exists and it contains data as well.

*Edit: So like I said it shouldn't be null as the data exists. And if it is null then I will need to know why is it null. Therefore, a question regarding what is System.NullReferenceException won't help me at all.

fishmong3r
  • 1,414
  • 4
  • 24
  • 51
  • 2
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Dmitry Jan 13 '16 at 15:27
  • @Dmitry Like I said it is not null. Or if it is then I don't know why. So I believe the question is valid. – fishmong3r Jan 13 '16 at 15:28
  • *I know for a fact that the value exists and it contains data as well.* How do you know that? Have you considered it could be an x86 key while your .NET app runs on the x64 CLR (or the other way around)? – Frédéric Hamidi Jan 13 '16 at 15:30
  • Exists both under "normal" path and under Wow6432Node. – fishmong3r Jan 13 '16 at 15:31
  • @Ian It does not make any difference. – fishmong3r Jan 13 '16 at 15:35
  • @fishmong3r ok, then it is not the case... – Ian Jan 13 '16 at 15:37
  • I'm not aware of any other reasons why the value would be null other than it doesn't exist, you don't have permission or you're looking in the "wrong" registry (x86/x64). [This post](http://stackoverflow.com/questions/13337577/setvalue-64bit-machine-registry) has a good example of making sure your registry code runs on both types of machine, the other two should be obvious to check. – Equalsk Jan 13 '16 at 15:39

2 Answers2

3

As raj's answer pointed out in this SO question, which is similar to yours, the problem could be that you're opening the registry on a 64bits OS.

Try this approach instead (.NET 4.0 or later) :

public class HKLMRegistryHelper
{

    public static RegistryKey GetRegistryKey()
    {
        return GetRegistryKey(null);
    }

    public static RegistryKey GetRegistryKey(string keyPath)
    {
        RegistryKey localMachineRegistry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32);

        return string.IsNullOrEmpty(keyPath) ? localMachineRegistry : localMachineRegistry.OpenSubKey(keyPath);
    }

    public static object GetRegistryValue(string keyPath, string keyName)
    {
        RegistryKey registry = GetRegistryKey(keyPath);
        return registry.GetValue(keyName);
    }
}

... and replace your code with :

string keyPath = @"SOFTWARE\Microsoft\CCM\LocationServices";
string keyName = "DnsSuffix";

var value = HKLMRegistryHelper.GetRegistryValue(keyPath, keyName);
Community
  • 1
  • 1
MartinVeronneau
  • 1,296
  • 7
  • 24
0

Reading the registry with "Registry.LocalMachine" can be unreliable as it defaults to the current application platform target (x86/x64) and when it's 64bit Registry.LocalMachine can view the key but can't access the data within.

Try specifying the view with RegistryKey.

var stuff = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
                .OpenSubKey("Software\\Microsoft\\CCM\\LocationServices", true);
var value = stuff.GetValue("DnsSuffix").ToString();
Seritin
  • 36
  • 4