C#操作配置文件中appSettings,connectionStrings节点「建议收藏」

C#操作配置文件中appSettings,connectionStrings节点「建议收藏」usingSystem;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Configuration;namespacemyConfiguration{   #region配置信息的操作类   ///   ///配置信息的操作   ///   publiccl

大家好,又见面了,我是你们的朋友全栈君。

   

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
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143565.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ubuntu虚拟机ip地址设置_网络虚拟ip地址怎么弄

    ubuntu虚拟机ip地址设置_网络虚拟ip地址怎么弄Ubuntu下配置虚拟IP地址

    2022年10月20日
    0
  • Vue 跨域配置「建议收藏」

    Vue 跨域配置「建议收藏」在package.json同级目录下新建文件vue.config.js;内容如下:module.exports={//自行复制黏贴proxyTable:{‘/java’:{//以/java开头的地址会被替换成下面的target,/java本身会被重写成下面的pathRewritetarget:’http://zaxytest.ciih.net/java’,//后台apichangeOrigin:t

    2022年7月23日
    6
  • scrapy ROBOTSTXT_OBEY 协议:「建议收藏」

    scrapy ROBOTSTXT_OBEY 协议:「建议收藏」

    2022年6月12日
    23
  • 文件管理学习:从百度网盘搬家onedrive测评「建议收藏」

    文件管理学习:从百度网盘搬家onedrive测评「建议收藏」网上已经有很多多家网盘对比的文章了。由于我平时依赖微软系列比较多,常用office,有自己的微软邮箱,科学上网跟吃饭一样(这克服了网页版被qiang的障碍)。所以最后我选择了onedrive,基于上述前提,搬家就是个一蹴而就的事。微软普通账户只有5G免费,要扩容得花钱至少15/月,然后查到了免费扩容的办法,那就是用edu邮箱注册。学校的edu邮箱终于有了用武之处。。用教育邮箱注册office…

    2022年9月4日
    4
  • SpringBoot自定义starters

    SpringBoot自定义startersSpringBoot自定义starters1、简介2、如何自定义starter1、简介SpringBoot最强大的功能就是把我们常用的场景 抽象成一个个starter(场景启动器),我们通过引入springBoot为我们提供这些场景启动器,我们再进行少量的配置就能使用相应的功能。但是,SpringBoot不能包含所有的场景,经常需要我们自定义starter,来简化我们对springBoot的使用。2、如何自定义starter…

    2022年10月23日
    0
  • 分子生物学数据库综合目录「建议收藏」

    分子生物学数据库综合目录「建议收藏」SRS序列查询系统http://www.embl-heidelberg.de/srs5/分子生物学数据库及服务器概览http://www.ai.sri.com/people/pkarp/mimbd/rsmith.htmlBioMedNet图书馆http://biomednet.comDBGET数据库链接http://www.genome.ad.jp/dbg

    2022年7月11日
    13

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号