57

I want to create 3 AppSettings config files:

  • Database.config
  • Messages.config
  • Global.config

And after add in my App.config:

<appSettings file="Database.config" />
<appSettings file="Messages.config" />
<appSettings file="Global.config" />

But when I try to access a key that there is in one of three files with the ConfigurationManager, I got the following error:

Configuration system failed to initialize. Sections must only appear once per config file.

I cannot have more than one AppSettings config file?

4
  • Define different config sections, why you want add extra files? appconfig file is one for all of running projects root. Jul 5, 2012 at 19:28
  • Most readable to final user that 'll change some values. Only one file, i'll have a big file, and i don't want it. Jul 5, 2012 at 19:37
  • You could also use some xml file to do your configuration, but config file with 200-300 lines of code is not bad;) Jul 5, 2012 at 19:39
  • also see stackoverflow.com/questions/17575144/… May 22, 2016 at 14:18

2 Answers 2

59

You can't have more than one appsettings because that's the name of a section. You can add a new section though that uses the same kind of section definition as appsettings. E.g.,

<configuration>
    <configSections>
        <section name="DatabaseConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </configSections>
    ....
    <DatabaseConfig>
       <add key="Whatever" value="stuff"/>
    </DatabaseConfig>
</configuration>
4
  • 1
    Can i add the <DatabaseConfig> in another file? My goal is put each config in a different file. Jul 5, 2012 at 19:27
  • Yep. Absolutely. Works exactly the same way as appsettings.
    – aquinas
    Jul 5, 2012 at 19:31
  • 1
    And to access with the configuration manager? How can i reach this? (because with appSettings i do: ConfigurationManager.AppSettings["MyKey"]) Jul 5, 2012 at 19:36
  • 16
    NameValueCollection settings = (NameValueCollection) ConfigurationManager.GetSection("DatabaseConfig");
    – aquinas
    Jul 5, 2012 at 20:13
49

Code for separate file:

Web.config:

<configSections>
    <section name="DatabaseConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <section name="MessageConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <section name="GlobalConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>

<DatabaseConfig configSource="database.config">
</DatabaseConfig>

<MessageConfig configSource="message.config">
</MessageConfig>

<GlobalConfig configSource="global.config">
</GlobalConfig>

database.config:

<DatabaseConfig>
  <add key="Name" value="ServerName" />
</DatabaseConfig>

etc...

Can be accessed via code like this:

var databaseConfiguration = (NameValueCollection)ConfigurationManager.GetSection("DatabaseConfig");
string name = databaseConfiguration["Name"];
1
  • this is exactly what i needed for app version
    – visual
    Sep 7, 2020 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.