代码之家  ›  专栏  ›  技术社区  ›  Dirk Brockhaus

从流而不是文件加载配置文件

  •  5
  • Dirk Brockhaus  · 技术社区  · 14 年前

    我将openmappedexenconfiguration与ExeConfigurationFileMap一起用于加载配置文件。它们的重载表明它们只处理文件名。有没有办法从流中加载配置文件?

    背景:我想加载作为嵌入式资源存储的配置文件。没有文件表示!

    2 回复  |  直到 14 年前
        1
  •  7
  •   Pieter van Ginkel    14 年前

    不。 Configuration 类来加载配置,而这个类实际上需要一个物理路径。

        2
  •  4
  •   Roman Pokrovskij Archil Labadze    8 年前

    对。 如果允许应用程序更改应用程序文件夹中的文件-更新 *.config update / save / refresh

    示例:假设您存储了wcf部分的组( <bindings> , <behaviors> .. 等)在文件中 wcfsections.test.config

            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);