C# 将GetValueNames()中的多个数组放入C中的一个数组中#
我有一个方法需要创建一个数组来保存根键的子键的名称,然后我需要另一个数组来保存所有这些子键中的值。我的困境是如何创建一个包含多个数组的多维数组 到目前为止,我的代码是:C# 将GetValueNames()中的多个数组放入C中的一个数组中#,c#,arrays,winforms,registry,C#,Arrays,Winforms,Registry,我有一个方法需要创建一个数组来保存根键的子键的名称,然后我需要另一个数组来保存所有这些子键中的值。我的困境是如何创建一个包含多个数组的多维数组 到目前为止,我的代码是: private void registryArrays() { //Two string arrays for holding subkeys and values string[] subKeys; string[] values = new string[];
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();
}
}
private void registryArrays(){
//用于保存子键和值的两个字符串数组
字符串[]子键;
字符串[]值=新字符串[];
//reg key用于访问根密钥CurrentUsers
RegistryKey regKey=Registry.CurrentUser;
注册表项regSubKey;
//从currentusers根键分配所有子键名称
subKeys=regKey.GetSubKeyNames();
for(int i=0;i
由于GetValueNames()会给我一个数组,所以我不知道该怎么做,但我需要得到多个数组,因为for循环将迭代我的根键的子键,所以我如何将所有数组放入一个数组?您不需要多个数组。使用字典可能更好。例如:
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只需复制/粘贴上面的示例,然后再试一次,如果您有一个特定的问题,而不是“不起作用”(它不会向我提供任何信息),我可能会尝试回答它。好的,在使用字典对您给我的代码进行一些调整之后,肯定是一个更好的方法。我感谢你的帮助!