27

The appsettings tag in the app.config has a file attribute:

<appSettings file="other.config">
..
..
</appSettings>

How does this work? Will it merge what is in the appSettings (original) with the other.config file? Or will it overwrite it? What if the other.config file doesn't exist, should it crash?

I'm trying it myself and if a key isn't in the original, it doesn't seem to read it from the other.config?

Should the other.config file have just xml nodes, or should it all be inside a appsettings element?

<appSettings>
  <userId>123</userId>
</appSettings>

or

<userId>123</userId>
codecompleting
  • 9,251
  • 13
  • 61
  • 102

2 Answers2

51
  • If the file doesn't exist it will not crash, it will just be ignored.
  • The external config has to contain the <appSettings> node so your first example is correct.
  • The value in the external file will take priority, if no value is present then the app.config value is used.

Does that cover off everything?

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
DoctorMick
  • 6,703
  • 28
  • 26
  • 1
    so in a winforms app, how would I reference it? `file=/bin/debug/other.config` ? – codecompleting Jan 24 '12 at 16:45
  • I haven't tried it but I'd suspect you don't need the path (as long as it is in the root of your project). – DoctorMick Jan 24 '12 at 16:47
  • 3
    it's actually doing the opposite, if the element is in the external config file, it uses that even if there is the same element in the app.config – codecompleting Jan 24 '12 at 16:54
  • 3
    That's what @DoctorMick said: values in the external file get priority. The values you specify in the Web.config itself only are used if there's no overriding value in the external config file. – Nate Cook May 21 '12 at 21:08
  • 5
    This answer is CORRECT but I was being thrown for a loop. Changes to external files are not detected by IIS Express, so your app does not automatically restart. Web.config changes are detected. This may fool you into thinking your external overrides are not being picked up. They ARE, but only when you change the web.config or rebuild! – Christopher Dec 15 '14 at 15:15
  • 1
    could you wire in multiple external files or are you limited to one? – VBAHole Jun 10 '16 at 19:09
  • I'm using Procmon to monitor file access to whatever file name I put in the file attribute. All I can say is that it's inconsistent, and as-if it's lagging behind. Bizarre and unreliable. – John Leidegren Feb 23 '17 at 10:01
  • The way this works is great if you have different environments. The main file will hold the default values and the external one may or may not override them as needed. Too bad the 'configSource' attribute in 'connectionStrings' works in exactly the opposite way.... I mean, what? – Okonomiyaki3000 Mar 30 '17 at 08:09
  • @Chris IIRC, you can alternately configure IIS to listen for changes to any file, including the external appSettings. – jpaugh Feb 24 '20 at 14:05
7

One of the best answers on the subject is here: ASP.NET web.config: configSource vs. file attributes - Credit to @Massimiliano Peluso

file attribute

configSource attribute

The file attribute specifies an external file containing custom settings like you do in the appSettings entry of the web.config file. Meanwhile, the external file specified in the configSource attribute contains the settings for the section which you declare the configSource for. For example, if you use the configSource attribute of the pages section, then the external file will contain the settings for the pages section.

The custom settings declared in the external config specifified in the file attribute will be merged with the settings in the appSettings section in the web.config file. In the meanwhile, the configSource does not support merging, it means that you'll have to move the entire section settings into the external file.

http://www.codeproject.com/Messages/1463547/Re-difference-between-configSource-and-file-attrib.aspx

JJS
  • 6,431
  • 1
  • 54
  • 70
  • 1
    Note: Don't forget to set `Copy to Output Directory` on any additional config files included in your source to ensure they appear in the expected location: https://stackoverflow.com/a/21092127/361842 – JohnLBevan Mar 16 '22 at 11:03