views:

37

answers:

2

I want to load a dictionary at startup in my console app from my app.config.

I know that I could use an xml library or linq to XML to load it to parse and traverse it. My question is there a BUILT IN way of doing it.

Isn't there some way to add an application configuration section into the app.config and then have it loaded automagically using ConfigurationManager class in the System.Configuration namespace?

Any example? BTW, I am in NET20.

EDIT
Sorry, I should have clarified. I want to load the dictionary WITHOUT using AppSettings. I know how to do that already. Of course, the downside of using AppSettings is that I have to change my code to add new values to my dictionary. That is why I am looking for a way to do it automatically.

+1  A: 

You will need to add an <appSettings> section to your app.config file. It will look something like:

<appSettings>
    <add key="foo" value="fooValue" />
    <add key="bar" value="barValue" />
    <add key="baz" value="bazValue" />
 </appSettings> 

From within your app, you can grab these values with System.Configuration.ConfigurationManager.AppSettings, which is a NameValueCollection, which is essentially a dictionary from string to string.

string myFoo = System.Configuration.ConfigurationManager.AppSettings["foo"];
Matt Greer
Matt,BTW I just realized I could use FOR EACH to get them all without modifying code. So thanks for the suggestion. To get them from a custom section would be even better though.
Seth Spearman
Mystagogue's solution might be more what you are after then. The .NET configuration API is quite extensive. IMO it's over engineered and complex, but chances are whatever you need can be found inside. I prefer to just stick with appSettings and the like as I rarely need much here, and the standard NameValueCollection implementations are pretty flexible. You can always use `NameValueCollection.AllKeys` to programatically get all the keys and very easily dump all of this into a `Dictionary<string, string>`
Matt Greer
+1  A: 

You can use the appSettings section the way you describe, but that section easily gets polluted with a variety of needs, and so I usually avoid it. You can make custom sections to deal with this.

Imagine you have a class called "PluginSpec," you can write code like this:

[ConfigurationCollection(typeof(PluginSpec), AddItemName = "Plugin",
    CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class PluginCollection : ConfigurationElementCollection
{
    //This collection is potentially modified at run-time, so
    //this override prevents a "configuration is read only" exception.
    public override bool IsReadOnly()
    {
        return false;
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new PluginSpec();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        PluginSpec retVal = element as PluginSpec;
        return retVal.Name;
    }

    public PluginSpec this[string name]
    {
        get { return base.BaseGet(name) as PluginSpec; }
    }

    public void Add(PluginSpec plugin){
        this.BaseAdd(plugin);
    }
}

The above code can be used from a member of another config class, like this:

    [ConfigurationProperty("", IsDefaultCollection = true)]
    public PluginCollection Plugins
    {
        get
        {
            PluginCollection subList = base[""] as PluginCollection;
            return subList;
        }
    }

The above would be a member in a class that derives from ConfigurationElement or ConfigurationSection.

Brent Arias