Skip to main content
 首页 » 编程设计

c#之如何将键值从字典保存到字符串

2024年02月06日22www_RR

我的词典有问题。我需要从那里获取值并将其保存到字符串中:

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)