Registry.GetValue always return null

后端 未结 6 1187
刺人心
刺人心 2020-12-08 15:26

I have the following key in my registry:

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

相关标签:
6条回答
  • 2020-12-08 16:05

    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

    0 讨论(0)
  • 2020-12-08 16:09

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-08 16:13

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

    0 讨论(0)
  • 2020-12-08 16:15

    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

    0 讨论(0)
  • 2020-12-08 16:23

    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();
    
    0 讨论(0)
  • 2020-12-08 16:26

    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.

    0 讨论(0)
提交回复
热议问题