C#对App.config中Settings和ConnectionStrings的增删改查

该代码提供了一个名为AppConfigHelper的C#类,用于对App.config文件中的Settings和ConnectionStrings部分进行添加、检查、修改和删除操作。类包含了针对appSettings的多种方法,如添加、检查是否存在、获取值、修改和删除项,以及针对ConnectionStrings的相应操作。
摘要由CSDN通过智能技术生成

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
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值