我的词典存在问题。我需要从那里获取值并将其保存为字符串:
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 密钥:名称,价值:NewCCReduced4.cone.bombardier.com 密钥:ipaddress,值:10.167.21.11 密钥:macaddress,值:000bab46edeb
现在您可以猜测我希望输出:名称NewCCReduced4.cone.bombardier.com将保存在String TestRackName中。
也许有人暗示我该如何继续?
答案 0 :(得分:3)
你几乎就在那里,只需放下[NewItemName]
:
foreach (var keyValue in newDictionary)
答案 1 :(得分:1)
var txt =string.Join(Environment.NewLine,
newDictionary
.Select (d => string.Format("{0}:{1}", d.Key,
string.Join(", ", d.Value.Select (d2 => string.Format("{0}:{1}", d2.Key, d2.Value))))));
没有循环,只有Console.Write(txt)
编辑 - 没有意识到这是一本字典词典