None of the solutions here worked for me, I was still getting null returned from my registry read. I finally found a solution which worked for me, based on an amalgamation of the answers above. Thanks to all for pointing me in the right direction.
I appreciate that I am late to the party, but I thought that this may help others if the above solutions do not work for them.
This function is part of a class:
/// <summary>
/// Gets the specified setting name.
/// </summary>
/// <param name="settingName">Name of the setting.</param>
/// <returns>Returns Setting if the read was successful otherwise, "undefined".</returns>
public static string get(string settingName)
{
RegistryKey key;
if (Environment.Is64BitOperatingSystem)
key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\MyCompany\MyProductName", false);
else
key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MyCompany\MyProductName", false);
try
{
String value = (String)key.GetValue(settingName);
return value;
}
catch
{
// Null is not returned as in this case, it is a valid value.
return "undefined";
}
finally
{
key.Close();
}
}