Tuesday, June 29, 2010

External config file access

I'm currently using an external config file to organise how settings in this project are running. Thusly, I have an entry in the web.config to the effect of:
<section name="acmeConstruction" type="System.Configuration.NameValueFileSectionHandler" />
and a reference to the external file:
<acmeConstruction configSource="App_Config\acmeConstruction.config"/>
containing
<acmeConstruction>
<add key="Homepage Videos" value="/sitecore/content/Home/Sitewide Content/Homepage Videos"/>
<add key="acmeConstruction" value="abc123"/>
</acmeConstruction>
Of course, now accessing said settings wasn't quite as easy to figure out.


It turns out that you can use
var nvc = ConfigurationManager.GetSection("acmeConstruction") as System.Collections.Specialized.NameValueCollection;


and access the result quite simply. It was surprising the amount of documentation I had to search to find this.