views:

445

answers:

5

Hi.

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.

A: 

Hmm... I know this is possible, because I know we were doing this at a previous job. I do not remember exactly how, though. I believe that we had a parent project that we were using to inherit the settings from, and there was a manual step by which if we made changes to the applicationSettings in a subproject, we needed to manually copy the changes to the parent project (but then they were inherited by all the child projects).

Sorry for the lack of specificity; I wasn't the one who implemented the solution, and it's gotten a bit fuzzy for me over time. I do recall that we were able to do this, though, and I think my recollections are accurate as to how we were doing it.

McWafflestix
+1  A: 

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.

Don
+2  A: 

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

marc_s
configSource doesn't work with applicationSettings. I get configuration error when I try to use it with applicationSettings.
empi
ah yes - that's a special case. You need to use "file=filename.config" instead.
marc_s
still doesn't work, I get default values from attribute [global::System.Configuration.DefaultSettingValueAttribute("Another setting value")] not values from the file specified
empi
Ah yes - the values from the file are the default, but can be overridden locally. So if you have the same key in both your external file and your app.config's appsettings section, your app.config wins. Nothing you can do about that, I'm afraid.
marc_s
+1  A: 

This question was not marked as answer for quite a long time. Well, under marc_s answer there are comments that explain that this is impossible. I didn't want to mark marc_s answer as accepted answer because the comments say exactly opposite thing than the answer. So the answer is that, unfortunately, it is not possible.

empi
A: 

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>
Dan F