69

I'd like to store a simple key/value string dictionary in my web config file. Visual Studio makes it easy to store a string collection(see sample below) but I'm not sure how to do it with a dictionary collection.

        <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <string>value1</string>
          <string>value2</string>
          <string>value2</string>
        </ArrayOfString>
TGnat
  • 3,903
  • 8
  • 35
  • 46

5 Answers5

130

Why reinvent the wheel? The AppSettings section is designed for exactly the purpose of storing dictionary-like data in your config file.

If you don't want to put too much data in your AppSettings section, you can group your related values into their own section as follows:

<configuration>
  <configSections>
    <section 
      name="MyDictionary" 
      type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>

  <MyDictionary>
     <add key="name1" value="value1" />
     <add key="name2" value="value2" />
     <add key="name3" value="value3" />
     <add key="name4" value="value4" />
  </MyDictionary>
</configuration>

You can access elements in this collection using

using System.Collections.Specialized;
using System.Configuration;

public string GetName1()
{
    NameValueCollection section =
        (NameValueCollection)ConfigurationManager.GetSection("MyDictionary");
    return section["name1"];
}
javamonkey79
  • 17,443
  • 36
  • 114
  • 172
Juliet
  • 80,494
  • 45
  • 196
  • 228
  • This is what I needed. Now, being a VB programmer I'd like to get it into the MySettings part of the web.config! – TGnat Dec 03 '08 at 19:13
  • @TGnat, do you have my.settings in webprojects? I dont. In winform-projects the app.config and my.settings are syncroniced against each other I think. – Stefan Dec 03 '08 at 21:55
  • Yes, The code sample I included in the question was copied from a My.Setting in the web.config. – TGnat Dec 04 '08 at 04:49
  • 1
    I tried to use the above code in my project, but ran into a problem when I included it after my element. Moving the custom stuff above appSettings seemed to fix it (not sure why). Also, I was able to drop the full type name and just use type="System.Configuration.NameValueFileSectionHandler". – Nogwater May 07 '09 at 19:38
  • 1
    I think the correct method call is ConfigurationManager.GetSection("MyDictionary"). – JefClaes Dec 29 '11 at 12:56
  • This doesn't work if you try to define MyDictionary in your appSettings file. For example, if you have two different configSource files for your appSettings, you can only use `` `` ``, not new sections like the you defined above. – styfle Dec 26 '12 at 22:01
  • The example give me the error "HTTP Error 500.19 - Internal Server Error". Apparently this no longer works in later versions of ASP – James Dec 09 '14 at 18:35
27

Juliet's answer is on point, but FYI you can also put additional configs in external .config files, by setting up your web.config as follows:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <!-- blah blah the default stuff here -->

    <!-- here, add your custom section -->
    <section name="DocTabMap" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>

  <!-- your custom section, but referenced in another file -->
  <DocTabMap file="CustomDocTabs.config" />

  <!-- etc, remainder of default web.config is here -->
</configuration>

Then, your CustomDocTabs.config looks like this:

<?xml version="1.0"?>
<DocTabMap>
  <add key="A" value="1" />
  <add key="B" value="2" />
  <add key="C" value="3" />
  <add key="D" value="4" />
</DocTabMap>

Now you can access it in code via:

NameValueCollection DocTabMap = ConfigurationManager.GetSection("DocTabMap") as NameValueCollection;
DocTabMap["A"] // == "B"
Factor Mystic
  • 26,279
  • 16
  • 79
  • 95
  • 1
    This is an excellent point. One tweak, however, is instead of specifying the attribute "file" I could only get it to work with the attribute "configSource." e.g. – Michael Krauklis Sep 22 '16 at 15:30
5

You would need to implement a custom section (See Configuration Section Designer).

What you really want... is something close to this:

<MyDictionary>
  <add name="Something1" value="something else"/>
  <add name="Something2" value="something else"/>
  <add name="Something3" value="something else"/>
</MyDictionary>

Where the XmlAttribute "name" is a Key which it won't allow to have more than one in the code behind. At the same time, make sure that the Collection MyDictionary is also a Dictionary.

You can do all of this with this tool and fill the gap as needed.

Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
  • Cant you use the Key-attribute instead of Name? I know I have a couple of those in the appsettings-section for some variables I use in the website. – Stefan Dec 03 '08 at 18:41
2

In application settings we can use System.Collection.Specilized.StringCollection

<X.Properties.Settings>
  <setting name="ElementsList" serializeAs="Xml">
    <value>
      <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <string>Element1</string>
        <string>Element2</string>
      </ArrayOfString>
    </value>
  </setting>
</X.Properties.Settings>

Access to list:

var element = Settings.Default.ElementsList[index]
christo
  • 671
  • 1
  • 8
  • 26
0

I'm not sure how to store a Dictionary directly but you could easily use an array of strings to store a dictionary. For every key, value pair you save out the key as the first string and the value as the second. Then when rebuilding the dictionary you can undo this encoding.

static Dictionary<string,string> ArrayToDictionary(string[] data) {
  var map = new Dictionary<string,string>();
  for ( var i=  0; i < data.Length; i+=2 ) {
    map.Add(data[i], data[i+1]);
  }
  return map;
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454