多个AppSettings的文件,这可能吗?多个、这可、文件、AppSettings

2023-09-04 01:53:18 作者:天使丢了翅膀怎样飞

我要创建3 AppSettings的配置文件:

I want to create 3 AppSettings config files:

Database.config Messages.config Global.config

和我的App.config中后添加:

And after add in my App.config:

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

但是,当我试图访问一个关键,有三种文件之一 ConfigurationManager中我得到了以下错误:

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.

我不能有一个以上的AppSettings的配置文件?

I cannot have more than one AppSettings config file?

推荐答案

您不能有一个以上的AppSettings,因为这是一个节的名称。您可以添加新的一节,虽然使用相同类型的部分定义的AppSettings。例如,

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>