0

I have been trying to work out why I can not access a value in the registry. I ended up with a possible solution but I would like to know why one works and the other doesn't and if I can go ahead and use this solution.

The original code:

RegistryKey regKey1 = Registry.LocalMachine.OpenSubKey(regPath);
string regValue1 = (string)regKey1.GetValue("CodeBase");

This fails because regKey1 is null.

A possible replacement (this works):

string regValue2 = (string)Registry.GetValue(Registry.LocalMachine.Name + regPath, "CodeBase", String.Empty);

According to this post there should be a different way of accessing the registry if the program is compiled for 32-bit and is running on a 64-bit machine. I am using a 64-bit version of Windows 7 and the program is being compiled for 'Any CPU'. However, if this was the reason then I would expect both solutions above to fail.

Can someone please explain the difference?

EDIT:

Found the problem.

regPath = @"\SOFTWARE\Wow6432Node\Classes\CLSID\ ...";

Because of the comment by David Heffernan I changed Registry.LocalMachine.Name + regPath into Path.Combine(Registry.LocalMachine.Name, regPath), but this didn't work to start with because regPath started with a '\' (the items were not combined - without an exception). Then I realized that OpenSubKey() also doesn't like the '\' at the start. After removing this from regPath both versions work the same. Thanks David and thanks also for your suggestions about using the RegistryView enum.

Community
  • 1
  • 1
Ben
  • 3,241
  • 4
  • 35
  • 49
  • 1
    Not an answer to your question, but a suggestion: You really shouldn't compile executables for "Any CPU", but either x86 OR amd64. You should use "Any CPU" only for class libraries. See http://blogs.msdn.com/b/rmbyers/archive/2009/06/09/anycpu-exes-are-usually-more-trouble-then-they-re-worth.aspx – chris Aug 12 '14 at 13:42
  • 1
    `Path.Combine(Registry.LocalMachine.Name, regPath)` is to be preferred – David Heffernan Aug 12 '14 at 13:45

1 Answers1

1

Version 1

If the key does not exist then regKey1 is null, and the call to regKey1.GetValue() fails for the obvious reason.

Version 2

If the key does not exist, Registry.GetValue() returns null, and regValue1 is therefore assigned null.


Regarding the issue of registry views and AnyCPU, you are asking for trouble at present. Presumably you mean to look in a specific registry view. You should be explicit about this by using the RegistryView enumeration.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks for your answer but I checked and version 2 is returning the contents of the key value correctly, it is not null or empty. – Ben Aug 12 '14 at 13:45
  • Then it is most likely the case that you aren't comparing like with like. So, most likely version 1 runs as a 32 bit process, and version 2 as a 64 bit process. Or vice versa. Try running them once after the other in the same process. – David Heffernan Aug 12 '14 at 13:49
  • Yup I did this the second version should have said `regValue2`, sorry for the typo. Can you explain further about how to explicitly specify the RegistryView please? – Ben Aug 12 '14 at 13:50
  • 1
    The MSDN documentation of `RegistryView` is pretty clear in my view – David Heffernan Aug 12 '14 at 13:51
  • Neither `RegistryKey.OpenSubKey(...)` or `Registry.GetValue(...)` accept the RegistryView as an argument. The documentation of [RegistryView](http://msdn.microsoft.com/en-us/library/microsoft.win32.registryview(v=vs.110).aspx) does not have a sample and it does not explain how to use it. So I am afraid I do not agree that the MSDN documentation is clear. However, I am sure there are some tutorials somewhere on this so I will go and Google it. – Ben Aug 12 '14 at 13:58
  • 1
    From the [docs](http://msdn.microsoft.com/en-us/library/microsoft.win32.registryview.aspx): *You can specify a registry view when you use the OpenBaseKey and OpenRemoteBaseKey(RegistryHive, String, RegistryView) methods, and the FromHandle property on a RegistryKey object.* In your case you would use `OpenBaseKey`. – David Heffernan Aug 12 '14 at 14:00