Opensubkey() Returns Null for a Registry Key That I Can See in Regedit.Exe

OpenSubKey() returns null for a registry key that I can see in regedit.exe

A 32-bit application on a 64-bit OS will be looking at the HKLM\Software\Wow6432Node node by default. To read the 64-bit version of the key, you'll need to specify the RegistryView:

using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
using (var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"))
{
// key now points to the 64-bit key
}

The API to do this was added in .NET 4.0; if you're still using 3.5, you'll need to use P/Invoke to access the 64-bit keys:
http://www.rhyous.com/2011/01/24/how-read-the-64-bit-registry-from-a-32-bit-application-or-vice-versa/

Registry.LocalMachine.OpenSubKey() returns null

In your comment to Dana you said you gave the ASP.NET account access. However, did you verify that that is the account that the site in running under? Impersonate and the anonymous access user can be easy to overlook.

UNTESTED CODE:

Response.Clear();  
Response.Write(Environment.UserDomainName + "\\" + Environment.UserName);
Response.End();

Registry OpenSubkey returns null, although it is there

As it seems, this is a security "feature". There is no way of accessing the registry branch of the addin, which is looking for it.
The information, which I would like to have stored there, needed to be stored somewhere else.

Why is OpenSubKey() returning null on my Windows 10 64-bit system?

It is probably not OpenSubKey() that is causing the null reference exception, but the baseKey.GetValue().ToString() call. The baseKey.GetValue() returns null (because in that case you are trying to get a value right under the HKEY_LOCAL_MACHINE root node) and you invoke ToString() on a null reference. Instead of baseKey.GetValue(), you should try key.GetValue(), assuming MASM32 is really a value under HKLM\SOFTWARE\Wow6432Node which is highly unlikely.

key.GetValue("MASM32").ToString();

Security Note: If you are looking for the installation path of MASM32, even though I do not have any expertise on that, they clearly state that The MASM32 SDK is registry safe and writes nothing to the registry.

Thus, MASM32 is probably a KEY not a VALUE, therefore please execute this method and print the output of it, and you will see the key/value pairs registered under the MASM32 key assuming it exists at the registry path HKLM\SOFTWARE\Wow6432Node\MASM32

public static string GetMASM32LocationFromRegistry()
{
RegistryKey localMachineRegistryKey;
RegistryKey masm32RegistryKey;
RegistryView currentRegistryView = RegistryView.Registry64;

localMachineRegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, currentRegistryView);
masm32RegistryKey = localMachineRegistryKey.OpenSubKey(@"SOFTWARE\Wow6432Node\MASM32");

if (masm32RegistryKey == null)
{
return @"ERROR: The registry key HKLM\SOFTWARE\Wow6432Node\MASM32 could not be found";
}

StringBuilder masm32RegistryKeyValuesBuilder = new StringBuilder("Key/Value pairs for registry key HKLM\\SOFTWARE\\Wow6432Node\\MASM32:\r\n");
foreach (string masm32RegistryKeyValueName in masm32RegistryKey.GetValueNames())
{
masm32RegistryKeyValuesBuilder.AppendFormat
(
"Key: [{0}], Value: [{1}], Value Type: [{2}]\r\n",
masm32RegistryKeyValueName,
masm32RegistryKey.GetValue(masm32RegistryKeyValueName),
masm32RegistryKey.GetValueKind(masm32RegistryKeyValueName)
);
}

return masm32RegistryKeyValuesBuilder.ToString();
}

NullReferenceException when trying to read a registry key with C#

LocalMachine corresponds to HKEY_LOCAL_MACHINE.

ClassesRoot represents HKEY_CLASSES_ROOT.

Try

RegistryKey key = Registry.ClassesRoot.OpenSubKey("apart\\Shell\\Open\\Command");

Why is OpenSubKey() returning null on my Windows 7 64-bit system?

It sounds like your unit testing project compiles to 64 bit. In the Compile settings of your unit testing project, set the "Target CPU" to x86 (instead of AnyCPU).



Related Topics



Leave a reply



Submit