C#配置文件读写(App.config)

 

 

/*************************************************
 * 描述:
 * 
 * Author:ys
 * Date:2023/10/18 16:14:32
 * Update:
 * ************************************************/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace demo.Helper
{
    /// <summary>
    /// 针对配置文件的读写操作 (App.config)
    /// ps:key和name都不区分大小写
    /// </summary>
    public class AppConfigHelper
    {
        Configuration _configuration;

        /// <summary>
        /// 打开当前的config
        /// </summary>
        public AppConfigHelper()
        {
            _configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        }

        /// <summary>
        /// 打开指定路径的config
        /// </summary>
        /// <param name="path"></param>
        public AppConfigHelper(string path)
        {
            _configuration = ConfigurationManager.OpenExeConfiguration(path);
        }

        /// <summary>
        /// 读取所有的AppSettings
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> ReadAllSettings()
        {
            Dictionary<string, string> dictKV = new Dictionary<string, string>();
            var settings = _configuration.AppSettings.Settings;
            foreach (string key in settings.AllKeys)
            {
                dictKV[key] = settings[key].Value;
            }
            return dictKV;
        }

        /// <summary>
        /// 读取AppSettings指定Key的Value
        /// </summary>
        /// <param name="key"></param>
        public string ReadSetting(string key)
        {
            var settings = _configuration.AppSettings.Settings;
            return settings[key].Value;
        }

        /// <summary>
        /// 修改AppSettings指定Key的值(没有就添加)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public bool AddUpdateSettings(string key, string value, bool noToAdd = true)
        {
            var settings = _configuration.AppSettings.Settings;
            if (settings[key] == null)
            {
                if (noToAdd)  //没有就添加
                {
                    settings.Add(key, value);
                }
                else
                {
                    return false;
                }
            }
            else
            {
                settings[key].Value = value;
            }
            _configuration.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(_configuration.AppSettings.SectionInformation.Name);
            return true;
        }

        /// <summary>
        /// 移除AppSettings指定Key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool RemoveSetting(string key)
        {
            var settings = _configuration.AppSettings.Settings;
            if (settings[key] == null)
            {
                return false;
            }
            settings.Remove(key);
            _configuration.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(_configuration.AppSettings.SectionInformation.Name);
            return true;
        }

        /// <summary>
        /// 读取所有的ConnectionStrings
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> ReadConnectionStrings()
        {
            Dictionary<string, string> dictKV = new Dictionary<string, string>();
            var connectionStrings = _configuration.ConnectionStrings.ConnectionStrings;
            foreach (ConnectionStringSettings key in connectionStrings)
            {
                dictKV[key.Name] = connectionStrings[key.Name].ConnectionString;
            }
            return dictKV;
        }

        /// <summary>
        /// 读取ConnectionStrings指定Name的ConnectionString
        /// </summary>
        /// <param name="name"></param>
        public string ReadConnectionString(string name)
        {
            var connectionStrings = _configuration.ConnectionStrings.ConnectionStrings;
            return connectionStrings[name].ConnectionString;
        }

        /// <summary>
        /// 修改ConnectionStrings指定Name的值(没有就添加)
        /// </summary>
        /// <param name="name"></param>
        /// <param name="connectionString"></param>
        public bool AddUpdateConnectionString(string name, string connectionString, bool noToAdd = true)
        {
            var connectionStrings = _configuration.ConnectionStrings.ConnectionStrings;
            if (connectionStrings[name] == null)
            {
                if (noToAdd)  //没有就添加
                {
                    connectionStrings.Add(new ConnectionStringSettings(name, connectionString));
                }
                else
                {
                    return false;
                }
            }
            else
            {
                connectionStrings[name].ConnectionString = connectionString;
            }
            _configuration.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(_configuration.ConnectionStrings.SectionInformation.Name);
            return true;
        }

        /// <summary>
        /// 移除ConnectionStrings指定Name
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool RemoveConnectionString(string name)
        {
            var connectionStrings = _configuration.ConnectionStrings.ConnectionStrings;
            if (connectionStrings[name] == null)
            {
                return false;
            }
            connectionStrings.Remove(name);
            _configuration.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(_configuration.ConnectionStrings.SectionInformation.Name);
            return true;
        }



    }
}

 

posted @ 2023-10-18 16:22  博客YS  阅读(89)  评论(0编辑  收藏  举报