windows 获取注册表项 C# 的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6089148/
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-09-15 16:54:13  来源:igfitidea点击:

Get value of registry key C#

c#.netwindowsnullregistry

提问by Hogan

I have already looked at existing topics, so please try to refrain from dropping links here.

我已经查看了现有主题,因此请尽量不要在此处删除链接。

I want to get the value of a registry key - plain and simple. Here is what I have so far.

我想获取注册表项的值 - 简单明了。这是我到目前为止所拥有的。

Registry:1) Made a key under

注册表:1)在下面创建一个键

Current_User\Software\Custom_Subkey\Custom_Value\Custom_key\string_value

Current_User\Software\Custom_Subkey\Custom_Value\Custom_key\string_value

I am trying to find the string_value

我试图找到string_value

        string reg_subKey = "Software\Custom_Subkey\Custom_Value";

        RegistryKey root = Registry.CurrentUser.CreateSubKey(reg_subKey);


        foreach (string keyname in root.GetValueNames())
        {
            textBox4.AppendText(keyname.ToString() + Environment.NewLine);

// Appends the following data to textBox4 once the foreach is completed:
// Header1
// Header2
// Header3
// Header4
// Header5

// Now I want to get the VALUES of each header:

            using (RegistryKey key = root.OpenSubKey(keyname))
            {

**// THIS LINE GETS HIGHLIGHTED WITH THE FOLLOWING ERROR:
"Object reference not set to an instance of an object.**"
                MessageBox.Show(key.ValueCount.ToString());
            }
        }

Hopefully this is a simple fix. I look forward to hearing your responses. Thanks, Evan

希望这是一个简单的修复。我期待着听到您的回应。谢谢,埃文

回答by Hogan

I believe you want root.GetSubKeyNames()in the loop not GetValueNames()

我相信你不想root.GetSubKeyNames()在循环中GetValueNames()

While values is working to get the values I would suggest the following loop:

虽然值正在努力获取值,但我建议使用以下循环:

foreach(string keyname in root.GetSubKeyNames())
{
    // use key to get value and set textbox4


    using (RegistryKey key = root.OpenSubKey(keyname))
    {
       MessageBox.Show(key.ValueCount.ToString());
    }
 }

回答by Cody Gray

The OpenSubKeymethoddoes not throw an exception if the specified subkey is not found. Instead, it simply returns null. It's your responsibility as a programmer to ensure that the appropriate key was found and opened by checking the return value of the method call.

如果未找到指定的子项,该OpenSubKey方法不会引发异常。相反,它只是返回null。作为程序员,您有责任通过检查方法调用的返回值来确保找到并打开了适当的键。

Thus, my suspicion is that the registry key that you've specified is invalid. Open up Registry Editor (regedt32.exe), and verify that you can find the key in the registry exactly as written.

因此,我怀疑您指定的注册表项无效。打开注册表编辑器 ( regedt32.exe),并确认您可以在注册表中找到与所写的完全相同的项。

If you find that the registry key is indeed located exactly where you thought it was, then the problem may be related to the WOW64 subsystem, which allows 64-bit versions of Windows to run 64-bit apps. If the value was written to the registry by a 32-bit program, you won't be able to read it with the above code from a 64-bit program (or vice versa). The simplest way to check this is to change the compilation settings for your project. For example, if you're currently compiling for x86, then change to compiling for x64, or vice versa. Registry redirectionmay also be getting in your way; this will check for that as well.

如果您发现注册表项确实位于您认为它所在的位置,那么问题可能与 WOW64 子系统有关,该子系统允许 64 位版本的 Windows 运行 64 位应用程序。如果该值是由 32 位程序写入注册表的,则您将无法使用上述代码从 64 位程序中读取它(反之亦然)。检查这一点的最简单方法是更改​​项目的编译设置。例如,如果您当前正在为 x86 编译,则更改为为 x64 编译,反之亦然。注册表重定向也可能妨碍您;这也将检查。

回答by gabriel290687

I wanted the very same thing and your code helped me, but as you said, it didn't work properly. So, I made some modifications and I think it works fine now! Try this:

我想要同样的东西,你的代码帮助了我,但正如你所说,它不能正常工作。所以,我做了一些修改,我认为它现在可以正常工作了!尝试这个:

//Just make the reference until "custom_subkey", not to the next one ("custom value")
string reg_subKey = "Software\Custom_Subkey";

RegistryKey root = Registry.CurrentUser.CreateSubKey(reg_subKey);

//Use GetSubKeyNames, instead of GetValueNames, because now you are in a higher level
foreach (string keyname in root.GetSubKeyNames())
{
    using (RegistryKey key = root.OpenSubKey(keyname))
    {
        foreach (string valueName in key.GetValueNames())
        {
            MessageBox.Show(valueName);
            MessageBox.Show(key.GetValue(valueName).ToString() );
        }
    }
}