C#对App.config中Settings和ConnectionStrings的增删改查
代码写成了一个helper类,直接上完整代码,有详细的注释
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SQLTool
{
/// <summary>
/// 对App.config的增删改查操作
/// </summary>
class AppConfigHelper
{
#region AppSettings的增删改查
/// <summary>
/// 添加键为keyName、值为keyValue的项
/// </summary>
/// <param name="keyName"></param>
/// <param name="keyValue"></param>
public static void AddItemAppSettings(string keyName, string keyValue)
{
//添加配置文件的项,键为keyName,值为keyValue
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add(keyName, keyValue);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
/// <summary>
/// 判断键为keyName的项是否存在
/// </summary>
/// <param name="keyName"></param>
/// <returns></returns>
public static bool ExistItemAppSettings(string keyName)
{
//判断配置文件中是否存在键为keyName的项
foreach (string key in ConfigurationManager.AppSettings)
{
if (key == keyName)
{
//存在
return true;
}
}
return false;
}
/// <summary>
/// 判断keyValue是否存在
/// 存在则返回key值
/// 不存在返回空字符串
/// </summary>
/// <param name="keyName"></param>
/// <returns></returns>
public static string ExistItemKeyValueAppSettings(string keyValue)
{
// 获取appSettings配置节
var appSettings = ConfigurationManager.AppSettings;
//判断配置文件中是否存在键为keyName的项
foreach (string key in appSettings.AllKeys)
{
if (appSettings[key].Contains(keyValue) && appSettings[key].Length - keyValue.Length < 2)
{
//存在
return key;
}
}
return "";
}
/// <summary>
/// 遍历获取所有 key为sql的items
/// </summary>
/// <returns></returns>
public static string[] GetItemKeyValueAppSettings()
{
// 获取appSettings配置节
var appSettings = ConfigurationManager.AppSettings;
List<string> result = new List<string>();
//判断配置文件中是否存在键为keyName的项
foreach (string key in appSettings.AllKeys)
{
if (key.Contains("sql"))
{
result.Add(appSettings[key]) ;
}
}
return result.ToArray();
}
/// <summary>
/// 获取键为keyName的项的值
/// </summary>
/// <param name="keyName"></param>
/// <returns></returns>
public static string ValueItemAppSettings(string keyName)
{
//返回配置文件中键为keyName的项的值
return ConfigurationManager.AppSettings[keyName];
}
/// <summary>
/// 修改键为keyName的项的值
/// </summary>
/// <param name="keyName"></param>
/// <param name="newKeyValue"></param>
public static void ModifyItemAppSettings(string keyName, string newKeyValue)
{
//修改配置文件中键为keyName的项的值
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings[keyName].Value = newKeyValue;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
/// <summary>
/// 删除键为keyName的项
/// </summary>
/// <param name="keyName"></param>
public static void RemoveItemAppSettings(string keyName)
{
//删除配置文件键为keyName的项
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Remove(keyName);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
#endregion
#region ConnectionStrings的增删改查
/// <summary>
/// ConnectionStrings 添加name为keyName、值为keyValue的项
/// </summary>
/// <param name="keyName"></param>
/// <param name="keyValue"></param>
public static void AddItemConnectionStrings(string keyName, string keyValue)
{
//添加配置文件的项,name为keyName,值为keyValue
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(keyName, keyValue));
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("ConnectionStrings");
}
/// <summary>
/// ConnectionStrings 判断name为keyName的项是否存在
/// </summary>
/// <param name="keyName"></param>
/// <returns></returns>
public static bool ExistItemConnectionStrings(string keyName)
{
//判断配置文件中是否存在name为keyName的项
foreach (ConnectionStringSettings key in ConfigurationManager.ConnectionStrings)
{
if (key.Name == keyName)
{
//存在
return true;
}
}
return false;
}
/// <summary>
/// ConnectionStrings 获取name为keyName的项的值
/// </summary>
/// <param name="keyName"></param>
/// <returns></returns>
public static string ValueItemConnectionStrings(string keyName)
{
//返回配置文件中name为keyName的项的值
return ConfigurationManager.ConnectionStrings[keyName]?.ConnectionString;
}
/// <summary>
/// ConnectionStrings 修改name为keyName的项的值
/// </summary>
/// <param name="keyName"></param>
/// <param name="newKeyValue"></param>
public static void ModifyItemConnectionStrings(string keyName, string newKeyValue)
{
//修改配置文件中name为keyName的项的值
RemoveItemConnectionStrings(keyName);
AddItemConnectionStrings(keyName, newKeyValue);
}
/// <summary>
/// ConnectionStrings 删除name为keyName的项
/// </summary>
/// <param name="keyName"></param>
public static void RemoveItemConnectionStrings(string keyName)
{
//删除配置文件name为keyName的项
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings.Remove(config.ConnectionStrings.ConnectionStrings[keyName]);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("ConnectionStrings");
}
#endregion
}
}