jpoh
jpoh

Reputation: 4606

Customizing app.config

I'd like to be able to extend ConfigurationManager so that I have an app.config some like the following:

<configuration>

<databases>
    <add name="db1" server="someServer" dbName="superDB" userName="" password=""/>
    <add name="db2" server="anotherServer" dbName="ordinaryDB" userName="dba" password="dba"/>
</databases>

</configuration>

And then to be able to access these fields via ConfigurationManager like so

string dbName = ConfigurationManager.Databases["db1"].DBName;

I've had a look at customization options available (here for instance) but it doesn't really give me what I'm trying to achieve. Is this even possible?

(I realise that I could do this by rolling my own configuration manager but I'd really prefer to extend what the .NET framework currently offers if at all possible)

Upvotes: 2

Views: 1087

Answers (2)

Eric Schoonover
Eric Schoonover

Reputation: 48412

It looks like you should use the ConfigurationManager.ConnectionStrings property in this case.

If you really do want to extend the App.Config to contain your own configuration section(s) you can create a class that derives from ConfigurationSection class. There is a good example here.

What you expressed in your question... to be able to do something like ConfigurationManager.Databases, where Databases is a custom static property on ConfigurationManager class, is not possible.

Upvotes: 2

Norbert B.
Norbert B.

Reputation: 5728

You don't have create a custom configuration manager. You can do this by creating an custom configuration section, registering it in the config file and you're done.

Hope this helps...

Ps good tutorial @ codeproject.com : http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx

Upvotes: 2

Related Questions