13

I like to split my app.config into a user specific part and an application specific part. Is it possible to store a part of the app.config in another file?

I already tried this:

<!DOCTYPE cruisecontrol [<!ENTITY email SYSTEM "email.config">]  >

but this does not load.

Is there another possiblity without changing the application itself?

schoetbi
  • 12,009
  • 10
  • 54
  • 72

1 Answers1

28

You can use the configSource attribute to tell the framework to load a particular section from another file.

For example, if you had a config file with a section like this:

<connectionStrings>
    <add name="MyDatabase" connectionString="...etc..." />
</connectionStrings>

You could replace it with:

  <connectionStrings configSource="ConnectionStrings.config" />

...and create a file ConnectionStrings.config with the contents of the original section (including the <connectionStrings> node - exactly the same as my first code section above).

Jon Grant
  • 11,369
  • 2
  • 37
  • 58
  • This is so great! I have been looking for a way to do this for so long, and it was right in front of me the entire time. – grimus Nov 03 '09 at 16:44
  • The problem is that this seems to only include connection strings, but does not help when you try to include configuration sections from other files. – Kjellski Sep 02 '13 at 12:55
  • 1
    @Kjellski I can assure you `configSource` works for all elements. Suggest you read up on how it works: http://weblogs.asp.net/fmarguerie/archive/2007/04/26/using-configsource-to-split-configuration-files.aspx – Jon Grant Sep 03 '13 at 12:51
  • @JonGrant You're absolutely right, but the most important thing to notice, is that you need the part `
    ` of the `` and then, are able to replace the `...` part with ``. But you're not able to place that attribute in the `` part of the `[web|app].config`.
    – Kjellski Sep 03 '13 at 13:04
  • if you use "file=" instead of "configSource= – BoldAsLove Jul 19 '22 at 18:52