Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the default config file?

Tags:

c#

.net

I'm using Jeff Atwood's Last Configuration Section Handler You'll Ever Need, but it only seems to work for the default app.config file. If I wanted to separate certain settings into another file, the deserializing doesn't work, since ConfigurationManager.GetSection only reads from the application's default app.config file. Is it possible to either change path of the default config file or point ConfigurationManager to a second config file?

like image 388
Lee Crabtree Avatar asked Jan 25 '26 01:01

Lee Crabtree


1 Answers

yes, just replace the section in the default config file with an xml element of the same name that has a configSource="" attribute that points to another file...

... In yr App.config or web.config...

  <configSections>
      <section name="Connections"
         type="BPA.AMP.Configuration.XmlConfigurator, BPA.AMP.Data.Config.DAL"/>
      <section name="AutoProcessConfig"
         type="BPA.AMP.Configuration.XmlConfigurator, BPA.AMP.Data.Config.DAL"/>
  </configSections>


  <Connections configSource="Config\Connections.config" />
  <AutoProcessConfig configSource="Config\AutoProcess.config" />

And then the common xml;Configurator class

   public class XmlConfigurator : IConfigurationSectionHandler
    {
        public object Create(object parent, 
                          object configContext, XmlNode section)
        {
            XPathNavigator xPN;
            if (section == null || (xPN = section.CreateNavigator()) == null ) 
                 return null;
            // ---------------------------------------------------------
            Type sectionType = Type.GetType((string)xPN.Evaluate
                                    ("string(@configType)"));
            XmlSerializer xs = new XmlSerializer(sectionType);
            return xs.Deserialize(new XmlNodeReader(section));
        }
    }
like image 93
Charles Bretana Avatar answered Jan 27 '26 17:01

Charles Bretana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!