Tag: 配置管理器

ConfigurationManager可以在Save()上保留XML注释吗?

我写了一个小实用程序,允许我为另一个应用程序的App.config文件更改一个简单的AppSetting,然后保存更改: //save a backup copy first. var cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile); cfg.SaveAs(cfg.FilePath + “.” + DateTime.Now.ToFileTime() + “.bak”); //reopen the original config again and update it. cfg = ConfigurationManager.OpenExeConfiguration(pathToExeFile); var setting = cfg.AppSettings.Settings[keyName]; setting.Value = newValue; //save the changed configuration. cfg.Save(ConfigurationSaveMode.Full); 这种效果很好,除了一个副作用。 新保存的.config文件会丢失所有原始XML注释,但仅在AppSettings区域内。 是否可以从原始配置文件AppSettings区域保留XML注释? 如果您想快速编译并运行它,那么这是完整源代码的pastebin。

有没有办法获得基于任意xml的System.Configuration.Configuration实例?

我正在尝试对我编写的自定义ConfigurationSection进行unit testing,并且我想将一些任意配置XML加载到每个测试的System.Configuration.Configuration中(而不是将测试配置xml放在Tests.dll中)。配置文件。也就是说,我想做这样的事情: Configuration testConfig = new Configuration(“…”); MyCustomConfigSection section = testConfig.GetSection(“mycustomconfigsection”); Assert.That(section != null); 但是,看起来ConfigurationManager只会为您提供与EXE文件或计算机配置关联的配置实例。 有没有办法将任意XML加载到Configuration实例中?