Reading keyvalue pairs into dictionary from app.config configSection

后端 未结 3 1403
北海茫月
北海茫月 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:37

    using ConfigurationManager class you can get whole section from app.config file as Hashtable which you can convert to Dictionary if you want to:

    var section = (ConfigurationManager.GetSection("DeviceSettings/MajorCommands") as System.Collections.Hashtable)
                     .Cast()
                     .ToDictionary(n=>n.Key.ToString(), n=>n.Value.ToString());
    

    you'll need to add reference to System.Configuration assembly

提交回复
热议问题