Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading keyvalue pairs into dictionary from app.config configSection

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

 <?xml version="1.0" encoding="utf-8" ?> <configuration>   <configSections>     <sectionGroup name="DeviceSettings">       <section name="MajorCommands" type="System.Configuration.DictionarySectionHandler"/>     </sectionGroup>   </configSections>   <appSettings>     <add key="ComPort" value="com3"/>     <add key="Baud" value="9600"/>     <add key="Parity" value="None"/>     <add key="DataBits" value="8"/>     <add key="StopBits" value="1"/>     <add key="Ping" value="*IDN?"/>     <add key="FailOut" value="1"/>   </appSettings>   <DeviceSettings>     <MajorCommands>       <add key="Standby" value="STBY"/>       <add key="Operate" value="OPER"/>       <add key="Remote" value="REMOTE"/>       <add key="Local" value="LOCAL"/>       <add key="Reset" value="*RST" />     </MajorCommands>   </DeviceSettings> </configuration> 

My current objective is to foreach or simply read all values from MajorCommands into a Dictionary<string, string> formatted as Dictionary<key, value>. I've tried several different approaches using System.Configuration but none seem to work and I haven't been able to find any details out there for my exact question. Is there any proper way to do this?

like image 624
DanteTheEgregore Avatar asked Jul 15 '13 20:07

DanteTheEgregore


2 Answers

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<System.Collections.DictionaryEntry>()                  .ToDictionary(n=>n.Key.ToString(), n=>n.Value.ToString()); 

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

like image 185
dkozl Avatar answered Sep 19 '22 21:09

dkozl


You are almost there - you just have nested your MajorCommands a level too deep. Just change it to this:

<configuration>   <configSections>     <section       name="MajorCommands"       type="System.Configuration.DictionarySectionHandler" />   </configSections>   <startup>     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />   </startup>   <MajorCommands>     <add key="Standby" value="STBY"/>     <add key="Operate" value="OPER"/>     <add key="Remote" value="REMOTE"/>     <add key="Local" value="LOCAL"/>     <add key="Reset" value="*RST" />       </MajorCommands> </configuration> 

And then the following will work for you:

var section = (Hashtable)ConfigurationManager.GetSection("MajorCommands"); Console.WriteLine(section["Reset"]); 

Note that this is a Hashtable (not type safe) as opposed to a Dictionary. If you want it to be Dictionary<string,string> you can convert it like so:

Dictionary<string,string> dictionary = section.Cast<DictionaryEntry>().ToDictionary(d => (string)d.Key, d => (string)d.Value); 
like image 25
Shaun McCarthy Avatar answered Sep 21 '22 21:09

Shaun McCarthy