开发手册 欢迎您!
软件开发者资料库

.Net(C#)通过代码修改保存AppConfig(Web.config)中SectionGroup配置项

本文主要介绍.NET(C#)中代码中操作AppConfig(Web.config)中SectionGroup配置项,通过代码将sectionGroup保存到配置中,实现sectionGroup配置项的嵌套。

1、保存sectionGroup到配置文件

/// ///  直接保存SectionGroup配置项/// public void SaveGroups(){    var configFilePath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;//vs调试时,获取的配置文件是"文件名.vshost.exe.Config",直接点击exe执行就正常了    Console.WriteLine("configFilePath = " + configFilePath);    var map = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };    var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);    var root = new ConfigurationSectionGroup();    config.SectionGroups.Add(ROOT_GROUP, root);    config.Save(ConfigurationSaveMode.Modified);    ConfigurationManager.RefreshSection(root.Name);    var nested = new UserSettingsGroup();    root.SectionGroups.Add(GROUP_1, nested);    nested = new UserSettingsGroup();    root.SectionGroups.Add(GROUP_2, nested);    config.Save(ConfigurationSaveMode.Modified);    ConfigurationManager.RefreshSection(root.Name);}

配置文件中生成的配置项:






2、保存sectionGroup配置项之前删除之前存在的

/// /// 保存之前,删除之前的SectionGroup配置项/// public void SaveGroup2(){    var configFilePath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;//vs调试时,获取的配置文件是"文件名.vshost.exe.Config",直接点击exe执行就正常了    Console.WriteLine("configFilePath = " + configFilePath);    var map = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };    var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);    var root = config.SectionGroups[ROOT_GROUP];    var existingGroups = new Dictionary();    while (root.SectionGroups.Count > 0)    {    existingGroups.Add(root.SectionGroups.Keys[0], root.SectionGroups[0]);    root.SectionGroups.RemoveAt(0);    }    config.Save(ConfigurationSaveMode.Modified);    existingGroups.Clear();    existingGroups.Add(GROUP_2, new UserSettingsGroup());    foreach (var key in existingGroups.Keys)    {    existingGroups[key].ForceDeclaration(true);    root.SectionGroups.Add(key, existingGroups[key]);    }    config.Save(ConfigurationSaveMode.Modified);    ConfigurationManager.RefreshSection(root.Name);}

配置文件中生成的配置项:


3、测试项目代码

using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            var a = new ConfigUtil();            a.SaveGroups();            a.SaveGroup2();                Console.ReadKey();        }    }    public class ConfigUtil    {        private string ROOT_GROUP = "ROOT_GROUP";        private string GROUP_1 = "GROUP_1";        private string GROUP_2 = "GROUP_2";        private Configuration GetConfig()        {            var configFilePath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;//vs调试时,获取的配置文件是"文件名.vshost.exe.Config",直接点击exe执行就正常了            Console.WriteLine("configFilePath = " + configFilePath);            var map = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };            var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);            return config;        }        ///         ///  直接保存SectionGroup配置项        ///         public void SaveGroups()        {            var config = GetConfig();            var root = new ConfigurationSectionGroup();            config.SectionGroups.Add(ROOT_GROUP, root);            config.Save(ConfigurationSaveMode.Modified);            ConfigurationManager.RefreshSection(root.Name);            var nested = new UserSettingsGroup();            root.SectionGroups.Add(GROUP_1, nested);            nested = new UserSettingsGroup();            root.SectionGroups.Add(GROUP_2, nested);            config.Save(ConfigurationSaveMode.Modified);            ConfigurationManager.RefreshSection(root.Name);        }        ///         /// 保存之前,删除之前的SectionGroup配置项        ///         public void SaveGroup2()        {            var config = GetConfig();            var root = config.SectionGroups[ROOT_GROUP];            var existingGroups = new Dictionary();            while (root.SectionGroups.Count > 0)            {                existingGroups.Add(root.SectionGroups.Keys[0], root.SectionGroups[0]);                root.SectionGroups.RemoveAt(0);            }            config.Save(ConfigurationSaveMode.Modified);            existingGroups.Clear();            existingGroups.Add(GROUP_2, new UserSettingsGroup());            foreach (var key in existingGroups.Keys)            {                existingGroups[key].ForceDeclaration(true);                root.SectionGroups.Add(key, existingGroups[key]);            }            config.Save(ConfigurationSaveMode.Modified);            ConfigurationManager.RefreshSection(root.Name);        }    }}