C# Registry.GetValue 总是返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9491958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:36:59  来源:igfitidea点击:

Registry.GetValue always return null

c#registry

提问by MoShe

I have the following key in my registry:

我的注册表中有以下键:

under:HKEY_LOCAL_MACHINE\SOFTWARE\RSAI have value object call - WebExControlManagerPathand its value is c:\

下:HKEY_LOCAL_MACHINE\SOFTWARE\RSA我有价值对象调用 -WebExControlManagerPath它的价值是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 rvalue is always null.

r值始终为空。

enter image description here

在此处输入图片说明

Any ideas?

有任何想法吗?

采纳答案by Jason

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:

您不能像在批处理脚本中那样访问 HKEY_LOCAL_MACHINE 配置单元。你打电话Registry.LocalMachine,这样:

        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.

如果它返回 null,请将您的构建架构设置为Any CPU。操作系统可能会以不同的方式虚拟化 32 位和 64 位注册表。请参阅:http: //msdn.microsoft.com/en-us/library/windows/desktop/aa965884%28v=vs.85%29.aspx从 32 位应用程序读取 64 位注册表,以及http://msdn.microsoft .com/en-us/library/windows/desktop/ms724072%28v=vs.85%29.aspx

回答by Ricky G

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

使用regedt32.exe查看注册表项的安全权限;检查您是否以管理员身份运行并关闭了 UAC。根据 opensubkey 文档,在访问任何密钥之前需要先打开它;http://msdn.microsoft.com/en-us/library/z9f66s0a.aspx

回答by Zeus

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

我的路径开头有额外的“\”,请确保设置正确。

回答by Palanikumar

The statementof Jasonis 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();

回答by Ahinoam Mazuz

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

如果您使用的是 64 位操作系统,当您尝试获取 HKEY_LOCAL_MACHINE\SOFTWARE\RSA它时实际上是在寻找HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\RSA这就是为什么您得到 null

回答by Ken Haynes

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();
    }
}