Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving Settings to another config file

Is it possible to move applicationSettings to another config file as it is possible with connectionStrings or appSettings?

When I create Settings for my web application using the designer I get applicationSettings section in my web.config such as:

  <applicationSettings>
    <TestWebApplication.Properties.Settings>
      <setting name="AnotherSetting" serializeAs="String">
        <value>Another setting value</value>
      </setting>
    </TestWebApplication.Properties.Settings>
  </applicationSettings>

I would like to be able to move them to another file like appSettings:

<appSettings configSource="config\appsettings.config"/>

I'm working with a project that has lots of settings accessed through class generated with designer and web.config is extremely hard to maintain between multiple environments. It would be even better if I could force Settings class to use appSettings not applicationSettings.

Is it possible?

Thanks in advance for help.

like image 646
empi Avatar asked May 04 '09 19:05

empi


3 Answers

Waaaay late for this question, but I've just been banging my head against the wall over this one. Turns out you can do this, just not for the entire applicationSettings node, you have to do each child node of the applicationSettings separately. I got the magic info from the bottom of the MSDN SectionInformation.ConfigSource article.

app.config/web.config contents

<applicationSettings>
    <TestWebApplication.Properties.Settings configSource="externalfile.config" />
</applicationSettings>

externalfile.config contents:

<TestWebApplication.Properties.Settings>
    <setting name="AnotherSetting" serializeAs="String">
        <value>Another setting value</value>
    </setting>
</TestWebApplication.Properties.Settings>
like image 61
Dan F Avatar answered Nov 03 '22 22:11

Dan F


Yes of course it's possible! Any ConfigurationSection in any of your config files can be "externalized" by means of the configSource="otherConfigFile.config" attribute.

It would be even better if I could force Settings class to use appSettings not applicationSettings.

In order to do that, you need to get away from using the nice visual "Settings" classes in .NET and use the ConfigurationManager directly. That way, you can put your settings into <appSettings> and read them in using ConfigurationManager.AppSettings["keyname"].

Marc

like image 34
marc_s Avatar answered Nov 03 '22 23:11

marc_s


If it were my project, I'd get rid of the applicationSettings and move everything to the appSettings section. The value of applicationSettings is that the values can be strongly typed and available to Intellisense. Neither of those are especially advantageous to your situation. Of course, that big hit of moving everything isn't worth it if you are still creating settings through the designer.

like image 1
Don Avatar answered Nov 03 '22 22:11

Don