28

I have the following key in my registry:

under:HKEY_LOCAL_MACHINE\SOFTWARE\RSA I have value object call - WebExControlManagerPath and its value is c:\

I am trying to do this:

var r = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\RSA", "WebExControlManagerPth",null);

if(r!=null)
    ProcessAsUser.Launch(ToString());

But r value is always null.

enter image description here

Any ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MoShe
  • 6,197
  • 17
  • 51
  • 77

8 Answers8

69

The statement of Jason is right, the operating system is the problem, the below code will help you to resolve.

RegistryKey localKey;
if(Environment.Is64BitOperatingSystem)
    localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
else
    localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);

string value = localKey.OpenSubKey("RSA").GetValue("WebExControlManagerPth").ToString();
Community
  • 1
  • 1
Palanikumar
  • 6,940
  • 4
  • 40
  • 51
  • 2
    Excellent fix without needing to compile for "Any CPU".. this should be the top answer. – dodgy_coder Jun 10 '15 at 02:14
  • 1
    @Ignatius, I didn't checked with below .NET 4.0. Thanks for your info :) – Palanikumar Aug 10 '15 at 11:07
  • Great fix. If you need support prior to .NET 4.0 check out this [answer](http://stackoverflow.com/a/28866237/3245111) to another post. – James R Apr 06 '17 at 13:17
  • This is the solution. – SpiDey Oct 21 '20 at 22:05
  • I'm paranoid about things like this, so I added this before the call to "localKey.OpenSubKey...": `if (localKey.GetSubKeyNames().Contains("RSA") && localKey.OpenSubKey("RSA").GetValueNames().Contains("WebExControlManagerPth")) value = localKey.OpenSubKey("RSA").GetValue("WebExControlManagerPth").ToString();` – Ted O'Connor Oct 11 '21 at 20:20
25

You don't access the HKEY_LOCAL_MACHINE hive the same way you do in C# as you would in batch scripting. You call Registry.LocalMachine, as such:

RegistryKey myKey = Registry.LocalMachine.OpenSubKey( @"Software\RSA", false);
String value = (String)myKey.GetValue("WebExControlManagerPth");

if (!String.IsNullOrEmpty(value))
{
    ProcessAsUser.Launch(ToString());
}

Update:

If it returns null, set your build architecture to Any CPU. The operating system may virtualize 32-bit and 64-bit registries differently. See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa965884%28v=vs.85%29.aspx, Reading 64bit Registry from a 32bit application, and http://msdn.microsoft.com/en-us/library/windows/desktop/ms724072%28v=vs.85%29.aspx.

FortyTwo
  • 2,414
  • 3
  • 22
  • 33
Jason
  • 6,878
  • 5
  • 41
  • 55
  • 1
    Do you have Administrative privileges? The Local Machine hive requires administrative privileges to write to. I'm not sure if administrative privileges are required for reading, but they might be the cause of your null string. JK, null for me too. – Jason Feb 29 '12 at 00:45
  • I run Visual studio as administrator – MoShe Feb 29 '12 at 00:48
  • WOrked!! Thanks ! what was the reason? – MoShe Feb 29 '12 at 00:54
  • 2
    Updated my post, too, for the reason. – Jason Feb 29 '12 at 00:55
6

if you are using 64 bit operating system, when you are trying to get HKEY_LOCAL_MACHINE\SOFTWARE\RSA it is actually looking for HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\RSA that is why you get null

Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
Ahinoam Mazuz
  • 73
  • 1
  • 6
  • 3
    It would be a better answer, if you also provide some solution along with this. If not, please include this as a comment to the question. – Prerak Sola Feb 09 '15 at 13:38
  • This is only true if the app is a 32-bit app, in which case the 64-bit OS will redirect reads and writes under `HKLM\SOFTWARE\Wow6432Node`. If the app is a 64-bit app, then 64-bit Windows will not redirect the writes. – Craig Tullis Mar 16 '18 at 00:02
2

I had this exact same issue; I found this thread.

For me, the solution was:

  1. VS2019 > Project > app Properties > Build > General > my Platform target: "Any CPU" was already selected
  2. *uncheck -- Prefer 32-bit

Admin only required for Registry.SetValue

Update: I learned something else today.

  1. The variable being assigned a value by Registry.GetValue must be static.
Dennis
  • 41
  • 5
  • I had the same problem, It ran the 32bit version and for some parts of the registry, it will not work. Many thanks – Taki7o7 Apr 20 '22 at 05:21
1

I had extra "\" in the beginning of my path, make sure that is set right.

Zeus
  • 3,091
  • 6
  • 47
  • 60
1

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();
    }
}
Ken Haynes
  • 105
  • 11
1

Following on from what Palanikumar commented above (i don't yet have enough rep to comment on his post sorry!) but i unticked 'prefer 32 bit' in my build options and this sorted my issue. When i was reading the registry value (in the 32 bit registry hive) it always came back as a 1 for a specific DWORD value when it was set as 0! Drove me up the wall figuring this out, tried rebooting, changing keys and nothing fixed it until i unticked this box. Now getting the correct 0 value back! Amazing!

string keyValue = key.GetValue("ChangeMeToKeyName").ToString();

                    if (keyValue == "0")
                    {
                        //DoThings
                    }
1

look at the security permissions on the registry key with regedt32.exe; check if you are running as admin and have UAC turned off. According to the opensubkey documentation it needs to be opened first before accessing any keys; http://msdn.microsoft.com/en-us/library/z9f66s0a.aspx

Ricky Gummadi
  • 4,559
  • 2
  • 41
  • 67