You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已加载默认App.config,如何加载并访问额外配置文件?

加载并访问额外配置文件的几种实用方案

没问题,咱们直接说在C#里加载额外配置文件的常用方法,都是项目里实打实能用的:

方案1:通过主App.config引入(零代码改动)

这是最省心的方式,你可以在默认的App.config里通过file属性直接关联额外配置文件,程序会自动合并两者的配置项。

操作步骤:

  1. 修改你的主App.config,给<appSettings>节点加上file属性指向额外配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings file="ExtraSettings.config">
    <add key="foo" value="bar"/>
  </appSettings>
</configuration>
  1. 创建额外配置文件ExtraSettings.config(放在主程序exe同目录即可),格式如下:
<appSettings>
  <add key="baz" value="qux"/>
  <!-- 要是想覆盖主配置里的项也可以,比如把foo改成newBar -->
  <!-- <add key="foo" value="newBar"/> -->
</appSettings>
  1. 读取配置的代码完全不用改,还是你原来的逻辑就能同时读取主配置和额外配置的项:
Configuration configManager = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
KeyValueConfigurationCollection config = configManager.AppSettings.Settings;
// 既能读主配置的foo,也能读额外文件的baz
string foo = config["foo"].Value;
string baz = config["baz"].Value;

小提示:如果额外文件的键和主配置重复,额外文件的项会覆盖主配置的

方案2:手动加载指定配置文件(灵活可控)

如果需要动态切换不同路径的配置文件,或者不想依赖主配置的关联,可以用ConfigurationManager.OpenMappedExeConfiguration手动加载。

代码示例:

// 创建配置映射,指定额外配置文件的路径(绝对/相对路径都可以)
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = @"C:\YourProject\ExtraSettings.config";

// 加载指定的配置文件
Configuration extraConfig = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

// 读取额外配置里的项
KeyValueConfigurationCollection extraSettings = extraConfig.AppSettings.Settings;
string baz = extraSettings["baz"].Value;

这种方式特别适合按环境(开发/测试/生产)切换配置的场景,你可以在代码里动态指定不同的配置文件路径。

方案3:自定义配置节加载结构化配置(复杂场景适用)

如果你的额外配置不是简单的键值对,而是有层级的结构化内容,可以自定义配置节来加载,虽然步骤多一点,但扩展性更强。

简单示例:

  1. 额外配置文件CustomConfig.config内容:
<configuration>
  <configSections>
    <section name="customSettings" type="YourNamespace.CustomSettingsSection, YourAssembly"/>
  </configSections>
  <customSettings>
    <dbConfig connectionString="server=xxx;db=xxx"/>
    <apiConfig baseUrl="https://xxx.com"/>
  </customSettings>
</configuration>
  1. 自定义配置节类:
public class CustomSettingsSection : ConfigurationSection
{
    [ConfigurationProperty("dbConfig")]
    public DbConfigElement DbConfig => (DbConfigElement)this["dbConfig"];

    [ConfigurationProperty("apiConfig")]
    public ApiConfigElement ApiConfig => (ApiConfigElement)this["apiConfig"];
}

public class DbConfigElement : ConfigurationElement
{
    [ConfigurationProperty("connectionString", DefaultValue = "")]
    public string ConnectionString => (string)this["connectionString"];
}

public class ApiConfigElement : ConfigurationElement
{
    [ConfigurationProperty("baseUrl", DefaultValue = "")]
    public string BaseUrl => (string)this["baseUrl"];
}
  1. 加载并读取:
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = @"C:\YourProject\CustomConfig.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

CustomSettingsSection customSection = (CustomSettingsSection)config.GetSection("customSettings");
string dbConn = customSection.DbConfig.ConnectionString;
string apiUrl = customSection.ApiConfig.BaseUrl;

你可以根据自己的实际需求选对应的方案:简单拆分键值对用方案1,动态切换用方案2,复杂结构化配置用方案3。

内容的提问来源于stack exchange,提问作者ryansin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:06:00