1、保存sectionGroup到配置文件
/// <summary>
/// 直接保存SectionGroup配置项
/// </summary>
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);
}
配置文件中生成的配置项:
<sectionGroup name="ROOT_GROUP">
<sectionGroup name="GROUP_1" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
<sectionGroup name="GROUP_2" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
</sectionGroup>
</sectionGroup>
2、保存sectionGroup配置项之前删除之前存在的
/// <summary>
/// 保存之前,删除之前的SectionGroup配置项
/// </summary>
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<string, ConfigurationSectionGroup>();
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);
}
配置文件中生成的配置项:
<sectionGroup name="ROOT_GROUP">
<sectionGroup name="GROUP_2" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
</sectionGroup>
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;
}
/// <summary>
/// 直接保存SectionGroup配置项
/// </summary>
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);
}
/// <summary>
/// 保存之前,删除之前的SectionGroup配置项
/// </summary>
public void SaveGroup2()
{
var config = GetConfig();
var root = config.SectionGroups[ROOT_GROUP];
var existingGroups = new Dictionary<string, ConfigurationSectionGroup>();
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);
}
}
}