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)
It looks like you should use the
ConfigurationManager.ConnectionStringsproperty 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
ConfigurationSectionclass. 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.