【发布时间】:2015-01-03 01:00:28
【问题描述】:
当我尝试访问已加密且无法正确解密的配置部分时(例如,有人只是盲目地从另一台机器上抓取配置文件)-Configuration 类抛出异常。我想抓住这种情况并在这种情况下完全重写该部分。
我已尝试删除并重新添加有问题的部分,但似乎忽略了删除 - 'catch' 中的第二条语句引发了另一个关于该部分已存在的异常:
try
{
// this getter might throw an exception - e.g. if encrypted on another machine
var connStrings = config.ConnectionStrings;
}
catch (ConfigurationException)
{
config.Sections.Remove("connectionStrings");
config.Sections.Add("connectionStrings", new ConnectionStringsSection());
}
这可能与我的 connectionStrings 部分位于单独的文件中这一事实有关,即我的配置文件有类似 <connectionStrings configSource="connections.config"/> 的内容,而实际加密的内容在 connections.config 文件中。
是否有可能只使用 .NET 配置类来完成我需要的操作而无需退回到直接的 XML 操作?
【问题讨论】:
标签: c# .net configurationmanager