Store config for individual ConfigurationElement in external .NET config file

12 Views Asked by At

I've seen that we can use the configSource attribute on certain sections in a .NET configuraton file. For example, connectionStrings, appSettings, etc. I'm looking for a way to do this on the level of an individual configurationElement.

For example, how might I get this config to work?

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="MyCustomSection" type="MySolution.MySectionHandler.MyCustomSection, MySolution.MySectionHandler" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
  </startup>
  <MyCustomSection>
    <Details>
      <detail configSource="ConfigFiles\detail1.config"/>
      <detail configSource="ConfigFiles\detail2.config"/>
    </Details>
  </MyCustomSection>
  <appSettings>
    <add key="key1" value="value1" />
    <add key="key2" value="value2" />
    <add key="key3" value="value3" />
  </appSettings>
</configuration>

I tried adding the configSource attribute to my custom ConfigurationElement class, but it turns out that attributes beginning with "config" are prohibited.

Any ideas for how to externalize config at this granular level?

0

There are 0 best solutions below