配置文件:与应用程序一起打包,是对应用程序的配置,方便管理。
一、基本操作,读写appSettings:
/// <summary>
/// 添加appsettings,如果appKey不存在则添加,存在则更新
/// </summary>
/// <param name="appKey"></param>
/// <param name="appValue"></param>
public static void AddConfig(string appKey, string appValue)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (ConfigurationManager.AppSettings[appKey] == null)
config.AppSettings.Settings.Add(appKey, appValue);
else
config.AppSettings.Settings[appKey].Value = appValue;
config.Save();
ConfigurationManager.RefreshSection("appSettings");
}
/// <summary>
/// 更新指定appKey的appsettings
/// </summary>
/// <param name="appKey"></param>
/// <param name="appValue"></param>
public static void UpdateConfig(string appKey, string appValue)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings[appKey].Value = appValue;
config.Save(); //ConfigurationSaveMode.Full
ConfigurationManager.RefreshSection("appSettings"); //刷新命名节,在下次检索它时将从磁盘重新读取它。记住应用程序要刷新节点
}
/// <summary>
/// 读取指定appKey的appsettings
/// </summary>
/// <param name="appKey"></param>
/// <returns></returns>
public static string ReadConfig(string appKey)
{
string read = ConfigurationManager.AppSettings[appKey];
return read;
}
/// <summary>
/// 删除指定appKey的appsettings
/// </summary>
/// <param name="appKey"></param>
public static void DeleteConfig(string appKey)
{
Configuration config=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (ConfigurationManager.AppSettings[appKey] != null)
{
config.AppSettings.Settings.Remove(appKey);
config.Save();
ConfigurationManager.RefreshSection("appSettings");
}
}
二、自定义操作
1、新建section类
class ConfigSectionData : ConfigurationSection
{
[ConfigurationProperty("key")]
public string Key
{
get { return (string)this["key"]; }
set { this["key"] = value; }
}
[ConfigurationProperty("vaule")]
public string Value
{
get { return (string)this["vaule"]; }
set { this["vaule"] = value; }
}
}
2、编写调用函数
/// <summary>
/// 读配置节点组value
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string ReadConfigGropValue(string sectionGroup, string name)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.GetSectionGroup(sectionGroup)!=null)
{
if (config.SectionGroups[sectionGroup].Sections[name] != null)
{
ConfigSectionData ReadGroupValue = config.SectionGroups[sectionGroup].Sections[name] as ConfigSectionData;
return ReadGroupValue.Value;
}
}
return null;
}
/// <summary>
/// 根据index读配置节点组value
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string ReadConfigGropValue(string sectionGroup, int index)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.GetSectionGroup(sectionGroup) != null)
{
ConfigSectionData ReadGroupValue = config.SectionGroups[sectionGroup].Sections[index] as ConfigSectionData;
return ReadGroupValue.Value;
}
return null;
}
/// <summary>
/// 读配置节点组key
/// </summary>
/// <param name="sectionGroup"></param>
/// <param name="name"></param>
/// <returns></returns>
public static string ReadConfigGropKey(string sectionGroup, string name)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.GetSectionGroup(sectionGroup) != null)
{
if (config.SectionGroups[sectionGroup].Sections[name]!=null)
{
ConfigSectionData ReadGroupValue = config.SectionGroups[sectionGroup].Sections[name] as ConfigSectionData;
return ReadGroupValue.Key;
}
}
return null;
}
/// <summary>
/// 根据Index读配置节点组key
/// </summary>
/// <param name="sectionGroup"></param>
/// <param name="name"></param>
/// <returns></returns>
public static string ReadConfigGropKey(string sectionGroup, int index)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.GetSectionGroup(sectionGroup) != null)
{
ConfigSectionData ReadGroupValue = config.SectionGroups[sectionGroup].Sections[index] as ConfigSectionData;
return ReadGroupValue.Key;
}
return null;
}
/// <summary>
/// 向配置节点组中(Settings)添加配置节点
/// </summary>
/// <param name="name"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void AddConfigGroup(string sectionGroup, string name, string key, string value)
{
int count = 0;
List<string> sectionName = new List<string>();
NameValueCollection nameCollection = new NameValueCollection();
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.SectionGroups[sectionGroup] != null)
{
count = config.SectionGroups[sectionGroup].Sections.Count;
for (int i = 0; i < count; i++)
{
ConfigSectionData temp = config.SectionGroups[sectionGroup].Sections[i] as ConfigSectionData;
sectionName.Add(config.SectionGroups[sectionGroup].Sections[i].SectionInformation.Name);
nameCollection.Add(temp.Key, temp.Value);
}
config.SectionGroups.Remove(sectionGroup);
config.Save();
}
config.SectionGroups.Add(sectionGroup, new ConfigurationSectionGroup());
for (int i = 0; i < count; i++)
{
ConfigSectionData da = new ConfigSectionData();
da.Key = nameCollection.GetKey(i);
da.Value = nameCollection.GetValues(i)[0];
config.SectionGroups[sectionGroup].Sections.Add(sectionName[i], da);
config.Save(ConfigurationSaveMode.Minimal);
}
if (!sectionName.Contains(name))
{
ConfigSectionData da1 = new ConfigSectionData();
da1.Key = key;
da1.Value = value;
config.SectionGroups[sectionGroup].Sections.Add(name, da1);
}
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection(sectionGroup);
}
/// <summary>
/// 读取节点组中secton个数
/// </summary>
public static int ReadGroupSectonCount(string sectionGroup)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.SectionGroups[sectionGroup] != null)
{
int count = config.SectionGroups[sectionGroup].Sections.Count;
return count;
}
else
{
return 0;
}
}
/// <summary>
/// 更新配置节点组中配置节点
/// </summary>
/// <param name="name"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void UpdateConfigGroup(string sectionGroup, string name, string key, string value)
{
int count = 0;
List<string> sectionName = new List<string>();
NameValueCollection nameCollection = new NameValueCollection();
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.SectionGroups[sectionGroup] != null)
{
count = config.SectionGroups[sectionGroup].Sections.Count;
for (int i = 0; i < count; i++)
{
ConfigSectionData temp = config.SectionGroups[sectionGroup].Sections[i] as ConfigSectionData;
sectionName.Add(config.SectionGroups[sectionGroup].Sections[i].SectionInformation.Name);
nameCollection.Add(temp.Key, temp.Value);
}
config.SectionGroups.Remove(sectionGroup);
config.Save();
}
else
{
config.SectionGroups.Add(sectionGroup, new ConfigurationSectionGroup());
ConfigSectionData da = new ConfigSectionData();
da.Key = key;
da.Value = value;
config.SectionGroups[sectionGroup].Sections.Add(name, da);
config.Save(ConfigurationSaveMode.Minimal);
count = 0;
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection(sectionGroup);
return;
}
config.SectionGroups.Add(sectionGroup, new ConfigurationSectionGroup());
for (int i = 0; i < count; i++)
{
if (sectionName[i] == name)
{
ConfigSectionData da = new ConfigSectionData();
da.Key = key;
da.Value = value;
config.SectionGroups[sectionGroup].Sections.Add(name, da);
config.Save(ConfigurationSaveMode.Minimal);
}
else
{
ConfigSectionData da = new ConfigSectionData();
da.Key = nameCollection.GetKey(i);
da.Value = nameCollection.GetValues(i)[0];
config.SectionGroups[sectionGroup].Sections.Add(sectionName[i], da);
config.Save(ConfigurationSaveMode.Minimal);
}
}
if (!sectionName.Contains(name))
{
ConfigSectionData da = new ConfigSectionData();
da.Key = key;
da.Value = value;
config.SectionGroups[sectionGroup].Sections.Add(name, da);
config.Save(ConfigurationSaveMode.Minimal);
}
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection(sectionGroup);
}
/// <summary>
/// 更新配置节点组中配置节点key,key和name绑定
/// </summary>
/// <param name="name"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void UpdateConfigGroupKey(string sectionGroup, string name, string key)
{
int count = 0;
List<string> sectionName = new List<string>();
NameValueCollection nameCollection = new NameValueCollection();
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.SectionGroups[sectionGroup] != null)
{
count = config.SectionGroups[sectionGroup].Sections.Count;
for (int i = 0; i < count; i++)
{
ConfigSectionData temp = config.SectionGroups[sectionGroup].Sections[i] as ConfigSectionData;
sectionName.Add(config.SectionGroups[sectionGroup].Sections[i].SectionInformation.Name);
nameCollection.Add(temp.Key, temp.Value);
}
config.SectionGroups.Remove(sectionGroup);
config.Save();
}
config.SectionGroups.Add(sectionGroup, new ConfigurationSectionGroup());
for (int i = 0; i < count; i++)
{
if (sectionName[i] == name)
{
ConfigSectionData da = new ConfigSectionData();
da.Key = key;
da.Value = nameCollection.GetValues(i)[0];
config.SectionGroups[sectionGroup].Sections.Add(key, da);
config.Save(ConfigurationSaveMode.Minimal);
}
else
{
ConfigSectionData da = new ConfigSectionData();
da.Key = nameCollection.GetKey(i);
da.Value = nameCollection.GetValues(i)[0];
config.SectionGroups[sectionGroup].Sections.Add(sectionName[i], da);
config.Save(ConfigurationSaveMode.Minimal);
}
}
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection(sectionGroup);
}
/// <summary>
/// 更新配置节点组中配置节点Value
/// </summary>
/// <param name="name"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void UpdateConfigGroupValue(string sectionGroup, string name, string value)
{
int count = 0;
List<string> sectionName = new List<string>();
NameValueCollection nameCollection = new NameValueCollection();
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.SectionGroups[sectionGroup] != null)
{
count = config.SectionGroups[sectionGroup].Sections.Count;
for (int i = 0; i < count; i++)
{
ConfigSectionData temp = config.SectionGroups[sectionGroup].Sections[i] as ConfigSectionData;
sectionName.Add( config.SectionGroups[sectionGroup].Sections[i].SectionInformation.Name);
nameCollection.Add(temp.Key, temp.Value);
}
config.SectionGroups.Remove(sectionGroup);
config.Save();
}
config.SectionGroups.Add(sectionGroup, new ConfigurationSectionGroup());
for (int i = 0; i < count; i++)
{
if (sectionName[i] == name)
{
ConfigSectionData da = new ConfigSectionData();
da.Key = nameCollection.GetKey(i);
da.Value = value;
config.SectionGroups[sectionGroup].Sections.Add(name, da);
config.Save(ConfigurationSaveMode.Minimal);
}
else
{
ConfigSectionData da = new ConfigSectionData();
da.Key = nameCollection.GetKey(i);
da.Value = nameCollection.GetValues(i)[0];
config.SectionGroups[sectionGroup].Sections.Add(sectionName[i], da);
config.Save(ConfigurationSaveMode.Minimal);
}
}
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection(sectionGroup);
}
/// <summary>
/// 更新配置节点组名
/// </summary>
/// <param name="name"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void UpdateConfigGroup(string sectionGroup, string newSectionGroup)
{
int count = 0;
List<string> sectionName = new List<string>();
NameValueCollection nameCollection = new NameValueCollection();
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.SectionGroups[sectionGroup] != null)
{
count = config.SectionGroups[sectionGroup].Sections.Count;
for (int i = 0; i < count; i++)
{
ConfigSectionData temp = config.SectionGroups[sectionGroup].Sections[i] as ConfigSectionData;
sectionName.Add(config.SectionGroups[sectionGroup].Sections[i].SectionInformation.Name);
nameCollection.Add(temp.Key, temp.Value);
}
config.SectionGroups.Remove(sectionGroup);
config.Save();
}
config.SectionGroups.Add(newSectionGroup, new ConfigurationSectionGroup());
for (int i = 0; i < count; i++)
{
ConfigSectionData da = new ConfigSectionData();
da.Key = nameCollection.GetKey(i);
da.Value = nameCollection.GetValues(i)[0];
config.SectionGroups[newSectionGroup].Sections.Add(sectionName[i], da);
config.Save(ConfigurationSaveMode.Minimal);
}
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection(newSectionGroup);
}
/// <summary>
/// 删除配置节点组中(Settings)配置节点
/// </summary>
/// <param name="name"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void DeleteGroupSection(string sectionGroup,string name)
{
int count = 0;
List<string> sectionName = new List<string>();
NameValueCollection nameCollection = new NameValueCollection();
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.SectionGroups[sectionGroup] != null)
{
count = config.SectionGroups[sectionGroup].Sections.Count;
for (int i = 0; i < count; i++)
{
ConfigSectionData temp = config.SectionGroups[sectionGroup].Sections[i] as ConfigSectionData;
sectionName.Add(config.SectionGroups[sectionGroup].Sections[i].SectionInformation.Name);
nameCollection.Add(temp.Key, temp.Value);
}
config.SectionGroups.Remove(sectionGroup);
config.Save();
}
config.SectionGroups.Add(sectionGroup, new ConfigurationSectionGroup());
for (int i = 0; i < count; i++)
{
if (sectionName[i] != name)
{
ConfigSectionData da = new ConfigSectionData();
da.Key = nameCollection.GetKey(i);
da.Value = nameCollection.GetValues(i)[0];
config.SectionGroups[sectionGroup].Sections.Add(sectionName[i], da);
config.Save(ConfigurationSaveMode.Minimal);
}
}
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection(sectionGroup);
}
/// <summary>
/// 删除配置节点组
/// </summary>
public static void DeleteConfigGroup(string sectionGroup)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.SectionGroups.Remove(sectionGroup);
//config.SectionGroups.Clear();
config.Save(ConfigurationSaveMode.Minimal);
}