我使用ExeConfigurationFileMap中的OpenMappedExeConfiguration来加载配置文件。他们的重载表明它们只适用于文件名。有没有一种方法可以从流中加载配置文件?
背景:我想要加载作为嵌入式资源存储的配置文件。没有文件表示!
我使用ExeConfigurationFileMap中的OpenMappedExeConfiguration来加载配置文件。他们的重载表明它们只适用于文件名。有没有一种方法可以从流中加载配置文件?
背景:我想要加载作为嵌入式资源存储的配置文件。没有文件表示!
问题:这个类本身不读取配置。文件路径最终由Configuration
类用于加载配置,而这个类实际上需要一个物理路径。
解决方案:我认为唯一的解决方案是将文件存储到临时路径并从那里读取。
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionCollection sections = ServiceModelSectionGroup.GetSectionGroup(config).Sections;
sections.Clear();
string fileName = ((GeneralSettings)ConfigurationManager.GetSection("generalSettings")).AppConfigServiceModelSectionFile;
XDocument doc = XDocument.Load(fileName);
var xmlGroup = (from x in doc.Descendants("system.serviceModel") select x).FirstOrDefault();
string[] sectionsInUpdateOrder = { "bindings", "comContracts", "behaviors", "extensions", "services", "serviceHostingEnvironment", "client", "diagnostics" };
foreach (string key in sectionsInUpdateOrder)
{
var e = (from x in xmlGroup.Elements(key) select x).FirstOrDefault();
if (e != null)
{
ConfigurationSection currentSection = sections[e.Name.LocalName];
string xml = e.ToString();
currentSection.SectionInformation.SetRawXml(xml);
}
}
config.Save();
foreach (string key in sectionsInUpdateOrder)
ConfigurationManager.RefreshSection("system.serviceModel/" + key);