C#操作配置文件中appSettings,connectionStrings节点

下面的代码是我看完周金桥老师的ASP.NET夜话,以及上网查了一些东西后,自己写的关于对配置文件中appSettings,connectionStrings节点的操作。关于配置文件的知识,我建议大家到周金桥老师的博客,去学习一下,我觉得他讲的较详细些,仅仅是一个建议。如果你对我写的代码有什么想法或者疑问,请给我发邮件或加我QQ(邮件:baiyu311@yahoo.cn QQ:602316022)

说明一下,我现在还是个学生,有些技术上的问题未必会懂,但我坚信通过我们的努力是可以客服它的。还有我最近在学习Linux,很想做一个Linux人,开源嘛,如果你有兴趣也可以联系我。期待每位上进的朋友,嘿嘿。。。。下面是我的代码:

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

namespace myConfiguration
{
    #region 配置信息的操作类
    /// <summary>
    /// 配置信息的操作
    /// </summary>
    public class ConfigurationOperator:IDisposable
    {
        #region 变量的声明
        /// <summary>
        /// Configuration Object
        /// </summary>
        private Configuration config;
        #endregion

        #region 构造函数,有参数(当前应用程序的虚拟路径)
        /// <summary>
        /// 构造函数,有参数(当前应用程序的虚拟路径)
        /// </summary>
        public ConfigurationOperator()
            : this(HttpContext.Current.Request.ApplicationPath)
        {

        }
        #endregion

        #region 构造函数,有参数(其他应用程序的虚拟路径)
        /// <summary>
        /// 构造函数,有参数(其他应用程序的虚拟路径)
        /// </summary>
        /// <param name="path">其他应用程序的虚拟路径</param>
        public ConfigurationOperator(string path)
        {
            config = WebConfigurationManager.OpenWebConfiguration(path);
        }
        #endregion

        #region 获取当前或其他应用程序配置文件中appSettings的所有keyName方法
        /// <summary>
        /// 定义获取当前或其他应用程序appSettings的所有keyName方法
        /// </summary>
        /// <returns>返回appSettings的所有keyName</returns>
        public string[] ActiveALLAppSettingsSection()
        {
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
            string[] appKeys = appSettings.Settings.AllKeys;
            return appKeys;
        }
        #endregion

        #region 设置当前或者其他应用程序配置文件中的appSettings节点
        /// <summary>
        /// 定义设置当前或者其他应用程序配置文件中的appSettings节点
        /// </summary>
        /// <param name="key">keyName</param>
        /// <param name="value">keyValue</param>
        public void SetAppSettingsSection(string key,string value)
        {
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
            if (appSettings.Settings[key]!=null)
            {
                appSettings.Settings[key].Value = value;
                this.Save();
            }
            else
            {
                appSettings.Settings.Add(key, value);
                this.Save();
            }
        }
        #endregion

        #region 删除当前或者其他应用程序配置文件中的appSettings节点
        /// <summary>
        /// 定义删除当前或者其他应用程序配置文件中的appSettings节点
        /// </summary>
        /// <param name="key">keyName</param>
        /// <returns>删除成功返回true,删除失败返回false</returns>
        public bool RemoveAppSettingsSection(string key)
        {
            AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
            if (appSettings.Settings[key] != null)
            {
                appSettings.Settings.Remove(key);
                this.Save();
                return true;
            }
            else
            {
                return false;
            }

        }
        #endregion

        #region 获取当前或其他应用程序配置文件中connectionStrings节点的所有ConnectionString
        /// <summary>
        /// 定义获取当前或其他应用程序配置文件中connectionStrings节点的所有ConnectionString
        /// </summary>
        /// <returns>返回connectionStrings节点的所有ConnectionString</returns>
        public string[] ALLConnectionStrings()
        {
            ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            ConnectionStringSettingsCollection conCollection = conSection.ConnectionStrings;
            string[] conStrings = new string[conSection.ConnectionStrings.Count];
            int i = 0;
            foreach (ConnectionStringSettings conSetting in conCollection)
            {
                conStrings[i++] = conSetting.ConnectionString;
            }
            return conStrings;
        }
        #endregion

        #region 设置当前或其他应用程序配置文件中ConnectionString节点
        /// <summary>
        /// 定义设置当前或其他应用程序配置文件中ConnectionString节点
        /// </summary>
        /// <param name="name">connectionStrings Name</param>
        /// <param name="ConnectionString">connectionStrings ConnectionString</param>
        /// <param name="providerName">connectionStrings ProviderName</param>
        public void SetConnectionStringsSection(string name, string ConnectionString, string providerName)
        {
            ConnectionStringsSection conSection=(ConnectionStringsSection)config.GetSection("connectionStrings");
            if (conSection.ConnectionStrings[name] != null)
            {
                conSection.ConnectionStrings[name].ConnectionString = ConnectionString;
                conSection.ConnectionStrings[name].ProviderName = providerName;
                this.Save();
            }
            else
            {

                ConnectionStringSettings conSettings = new ConnectionStringSettings(name, ConnectionString, providerName);
                conSection.ConnectionStrings.Add(conSettings);
                this.Save();
            }
        }
        #endregion

        #region 删除当前或其他应用程序配置文件中ConnectionString节点
        /// <summary>
        /// 定义删除当前或其他应用程序配置文件中ConnectionString节点
        /// </summary>
        /// <param name="name">ConnectionStrings Name</param>
        /// <returns>删除成功返回true,删除失败返回false</returns>
        public bool RemoveConnectionStringsSection(string name)
        {
            ConnectionStringsSection conSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            if (conSection.ConnectionStrings[name] != null)
            {
                conSection.ConnectionStrings.Remove(name);
                this.Save();
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 保存配置文件,并重新赋值config为null
        /// <summary>
        /// 定义保存配置文件的方法
        /// </summary>
        public void Save()
        {
            config.Save();
            config = null;
        }
        #endregion

        #region 释放配置文件对象
        /// <summary>
        /// 释放配置文件对象
        /// </summary>
        public void Dispose()
        {
            if (config != null)
            {
                config.Save();
            }
        }
        #endregion
    }
    #endregion
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值