【问题标题】:Putting multiple arrays from GetValueNames() into one array in C#将 GetValueNames() 中的多个数组放入 C# 中的一个数组中
【发布时间】:2013-06-19 21:24:19
【问题描述】:

我有一个方法需要创建一个数组来保存根键的子键的名称,然后我需要另一个数组来保存所有这些子键中的值。我的困境是如何创建一个包含多个数组的多维数组?

到目前为止我的代码:

    private void registryArrays() {

        //Two string arrays for holding subkeys and values
        string[] subKeys;
        string[] values = new string[];

        //reg key is used to access the root key CurrentUsers
        RegistryKey regKey = Registry.CurrentUser;
        RegistryKey regSubKey;

        //Assign all subkey names from the currentusers root key
        subKeys = regKey.GetSubKeyNames();

        for (int i = 0; i < subKeys.Length; i++) {

            regSubKey = regKey.OpenSubKey(subKeys[i]);
            values[i] = regSubKey.GetValueNames();

        }
    }

我不知道该怎么做,因为 GetValueNames() 会给我一个数组,但是我需要获取多个数组,因为 for 循环将遍历我的根键的子键,所以我将如何把所有将数组合并为一个数组?

【问题讨论】:

    标签: c# arrays winforms registry


    【解决方案1】:

    您不需要多个数组。使用字典可能会更好。例如;

    RegistryKey regKey = Registry.CurrentUser;
    var console = regKey.OpenSubKey("Console");
    var dict = console.GetValueNames()
              .ToDictionary(key => key, key => console.GetValue(key));
    
    
    foreach (var kv in dict)
    {
        Console.WriteLine(kv.Key + "=" + kv.Value);
    }
    

    【讨论】:

    • 我尝试实现这个,但 ToDictionary 不适用于数组。或者至少这是我遇到的问题。
    • @JBeck 只需复制/粘贴上面的示例,然后尝试,如果您有 “不起作用”以外的特定问题 (没有给我任何信息),我可能会尝试回答。
    • 好的,在对您给我的代码进行一些调整之后,使用字典绝对是一种更好的方法。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多