【问题标题】:Switching between config files in C#在 C# 中的配置文件之间切换
【发布时间】:2023-03-21 08:05:01
【问题描述】:

这里我有两个config文件,我需要在C#中的这些config文件之间切换

例子:

  1. app.config
  2. address.config

我需要在运行时将app.config 更改为address.config 以获取数据。

我尝试了以下代码:

System.Configuration.Configuration config
    = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        config.AppSettings.File = runtimeconfigfile;
        config.Save(ConfigurationSaveMode.Modified);
        ConfigurationManager.RefreshSection("appSettings");

【问题讨论】:

  • 为什么?为什么需要在执行过程中更改配置?
  • 你能不只是将“address.config”加载为一个xml文件并在不卸载app.config的情况下读取你需要的节点吗?
  • 我需要该配置文件中的一些数据。
  • 那为什么不把它作为你的app.config 的一部分呢?如果必须是单独的文件,还是使用configSource
  • 您只等了 18 分钟,其他人免费回答了您的问题,而您在抱怨吗?难以置信!

标签: c# app-config configuration-files


【解决方案1】:

这应该可行:

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = strConfigPath };
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

当你想切换文件时,你可以改变ExeConfigFileName,重新打开映射配置。

【讨论】:

  • 这是你的配置文件所在位置的路径。
猜你喜欢
  • 2022-12-25
  • 1970-01-01
  • 1970-01-01
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多