views:

194

answers:

3

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)

+2  A: 

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

norbertB
just 4 the good tutorial
almog.ori
+1  A: 

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.

spoon16
Good point about connect strings. It did not come to mind despite the fact that I would use the information I want configured to come up with effectively a set of connect strings. Whether or not defining connect strings is more difficult then simply specifying the server, db name, etc for the user is another question I guess. Thanks!
jpoh
A: 

Hi,

I would like to know whether there is any way to maintain the config data in memory, say in some Dictionary and be able to read/write through the ConfigurationManager class. My basic requirement is to skip the configuration reads/writes to the file system, but to be able to use the in memory structures.

Rams
Ask a proper question! Putting your question in an answer like this will not get you anywhere.
jpoh