如何更改远程配置文件中的配置源值



我正在使用ConfigurationManager.OpenMappedExeConfiguration来读取和修改远程Web服务的web.config文件。这在大多数情况下效果很好。配置文件

使用
    <unity configSource="Unity1.config"/>

如何将其更改为指向 Unity2.config?我试过了

    Config.Sections["unity"].SectionInformation.ConfigSource = "Unity2.config"

这确实会更新 web.config 文件。但是,这也会导致 Unity2.config 被 Unity1.config 的内容覆盖,这不是我想要的。

另外,有没有办法刷新以这种方式打开的配置对象?

var config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
var section = config.GetSection(UnityConfigurationSection.SectionName) as UnityConfigurationSection;
if (section == null)
{
     section = new UnityConfigurationSection();
     config.Sections.Add(UnityConfigurationSection.SectionName, section);
}
section.SectionInformation.ConfigSource = "unity.config";
config.Save(ConfigurationSaveMode.Full);

工作正常

最新更新