首页 > 技术文章 > LIB 配置文件读写器

pengzhen 2014-06-18 14:13 原文

    由于读写配置文件的次数比较频繁,而且拥有众多的类库,到最后,直接被各种各样的类库烦死。

    顺手封一个简单,实用的。主要功能:

  • 读写AppSetting
  • 读取连接字符串
  • 读取自定义配置节

 

using System;
using System.Configuration;
using System.Web.Configuration;

namespace HPWorkUtility
{
    /// <summary>
    /// 配置文件读写器
    /// </summary>
    public class ConfigReaderWriter
    {
        /// <summary>
        /// 获取指定的AppSetting
        /// </summary>
        /// <param name="key">键的名称</param>
        /// <returns>键值</returns>
        public static string GetAppSetting(string key)
        {
            return ConfigurationManager.AppSettings[key];
        }

        /// <summary>
        /// 获取指定的AppSetting
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">键的名称</param>
        /// <returns>键值</returns>
        public static T GetAppSetting<T>(string key)
        {
            var val = GetAppSetting(key);
            return (T)Convert.ChangeType(val, typeof(T));
        }

        /// <summary>
        /// 设置AppSetting节的值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="isWebConfig">是否是WebConfig文件</param>
        public static void SetAppSetting(string key, string value, bool isWebConfig)
        {
            //增加的内容写在appSettings段下 <add key="RegCode" value="0"/>
            Configuration config = null;
            if (isWebConfig)
            {
                config = WebConfigurationManager.OpenWebConfiguration("~");
            }
            else
            {
                config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            }
            if (config.AppSettings.Settings[key] == null)
            {
                config.AppSettings.Settings.Add(key, value);
            }
            else
            {
                config.AppSettings.Settings[key].Value = value;
            }
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件 
        }

        /// <summary>
        /// 读取自定义配置节
        /// </summary>
        /// <param name="sectionName">配置节的名称</param>
        /// <returns>配置对象</returns>
        public static object GetCustomSection(string sectionName)
        {
            return ConfigurationManager.GetSection(sectionName);
        }

        /// <summary>
        /// 读取自定义配置节
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="sectionName">配置节的名称</param>
        /// <returns>配置对象</returns>
        public static T GetCustomSection<T>(string sectionName) where T : class
        {
            return GetCustomSection(sectionName) as T;
        }

        /// <summary>
        /// 读取连接字符串
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns>连接字符串</returns>
        public static string GetConnectionString(string name)
        {
            return GetConnectionStringSetting(name).ConnectionString;
        }

        /// <summary>
        /// 读取连接字符串设置对象
        /// </summary>
        /// <param name="name">名称</param>
        /// <returns>设置对象</returns>
        public static ConnectionStringSettings GetConnectionStringSetting(string name)
        {
            return ConfigurationManager.ConnectionStrings[name];
        }
    }
}

    Lib中的,将会持续改进和更新~

推荐阅读