Reading keyvalue pairs into dictionary from app.config configSection

后端 未结 3 1406
北海茫月
北海茫月 2021-01-30 17:13

I currently have an app.config in an application of mine set up like so:

 

  

        
3条回答
  •  攒了一身酷
    2021-01-30 17:23

    I would probably treat the config file as an xml file.

    Dictionary myDictionary = new Dictionary();
    XmlDocument document = new XmlDocument();
    document.Load("app.config");
    XmlNodeList majorCommands = document.SelectNodes("/configuration/DeviceSettings/MajorCommands/add");
    
    foreach (XmlNode node in majorCommands)
    {
        myDictionary.Add(node.Attributes["key"].Value, node.Attributes["value"].Value)
    }
    

    If document.Load doen't work, try converting your config file to xml file.

提交回复
热议问题