将GetValueNames()中的多个数组放入c#中的一个数组中

本文关键字:数组 一个 GetValueNames | 更新日期: 2023-09-27 18:19:11

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

到目前为止我的代码:

    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循环将通过我的根键的子键迭代,所以我将如何把所有数组到一个数组?

将GetValueNames()中的多个数组放入c#中的一个数组中

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

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