我的词典有问题。我需要从那里获取值并将其保存到字符串中:
class CTestRack
{
public String TestRackName { get; set; }
public String NetworkName { get; set; }
public String IPAddress { get; set; }
public String MacAddress { get; set; }
public string NewItemName;
public void GetSessionName()
{
FileIniDataParser fileParser = new FileIniDataParser();
IniData data = fileParser.ReadFile("config.ini");
Dictionary<string, Dictionary<string, string>> newDictionary = new Dictionary<string, Dictionary<string, string>>();
Form1 _form1Object = new Form1();
foreach (SectionData section in data.Sections)
{
var keyDictionary = new Dictionary<string, string>();
foreach (KeyData key in section.Keys)
keyDictionary.Add(key.KeyName.ToString(), key.Value.ToString());
newDictionary.Add(section.SectionName.ToString(), keyDictionary);
}
if (_form1Object.comboBox2.SelectedIndex.ToString() != null)
{
System.Threading.Thread.Sleep(1000);
foreach (var keyValue in newDictionary[NewItemName])
{
Console.WriteLine("Key: {0}, Value: {1}", keyValue.Key, keyValue.Value);
}
}
}
所以基本上输出是:
NewCCReduced4 Key: name, Value: NewCCReduced4.cone.bombardier.com Key: ipaddress, Value: 10.167.21.11 Key: macaddress, Value: 000bab46edeb
现在您可以猜到我想要输出:Name NewCCReduced4.cone.bombardier.com 将保存在字符串 TestRackName 中。
也许有人提示我应该如何继续?
最佳答案
你就快完成了,只需放下 [NewItemName]
:
foreach (var keyValue in newDictionary)
关于c# - 如何将键值从字典保存到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25886442/