开发者

How do you load a dictionary into a console application from an app.config?

开发者 https://www.devze.com 2023-01-11 23:06 出处:网络
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

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.


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"];


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号