Joseph Michael Pesch
VP Programming

C# App.Config Custom Dictionary Section to Store Simple Key/Value Pairs

by 4. November 2013 06:12

Below is a sample app.config using a custom dictionary section to store simple key/value pairs:

    <section name="CustomDictionarySection" type="System.Configuration.DictionarySectionHandler"/>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    <add key="Key1" value="Value 1" />
    <add key="Key2" value="Value 2" />
    <add key="Key3" value="Value 3" />
class Program
  static void Main(string[] args)
    string key = "Key2";
    var section = (ConfigurationManager.GetSection("CustomDictionarySection") as System.Collections.Hashtable)
                .Where(t => t.Key.ToString() == key);
    if (section.Count() > 0)
      Console.WriteLine(string.Format("Found section with key: {0} containing value: {1}", key, section.First().Value));
      Console.WriteLine(string.Format("Could not find section with key: {0}", key));



Comments are closed