获取注册表项C#的值

5 .net c# windows registry null

我已经查看了现有主题,因此请尽量避免在此处删除链接.

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

注册表: 1)下了一把钥匙

CURRENT_USER\SOFTWARE\Custom_Subkey\Custom_Value\Custom_key\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());
            }
        }
Run Code Online (Sandbox Code Playgroud)

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

Hog*_*gan 8

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

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

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());
    }
 }
Run Code Online (Sandbox Code Playgroud)