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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • CFileDialog的使用方法简单介绍

    CFileDialog的使用方法简单介绍

    2021年12月1日
    45
  • 100G光模块有什么优势

    100G光模块有什么优势

    2021年7月6日
    85
  • maven快速入门_maven如何使用

    maven快速入门_maven如何使用企业级架构框架图之前我们关注的是前端的解决方案(涉及到的技术有H5、CSS3、JavaScript,CSS升级为Bootstrap再升级到ElementUI,JavaScript升级到jQuery再升级到Vue+NodeJS)现在开始我们开始关注后端的解决方案,也就是服务器端到底干了什么,哪些技术来支持(SpringBoot、Maven、SpringMVC、Spring、Mybatis)。这样前后端都学习完,整个软件项目所需要的基本技术就全线贯通,就可以自己独立完成企业级项目的开发了。下面我们来描

    2022年8月22日
    5
  • HibernateTemplate常用方法 .[通俗易懂]

    HibernateTemplate常用方法 .[通俗易懂]HibernateTemplate提供非常多的常用方法来完成基本的操作,比如通常的增加、删除、修改、查询等操作,Spring2.0更增加对命名SQL查询的支持,也增加对分页的支持。大部分情况下,使用Hibernate的常规用法,就可完成大多数DAO对象的CRUD操作。下面是HibernateTemplate的常用方法简介:qvoiddelete(Objecten…

    2022年6月16日
    23
  • 超详细的CentOS7.4下载与图文安装

    超详细的CentOS7.4下载与图文安装一、CentOS7.4下载官网下载地址:http://vault.centos.org/1、进入CentOS下载官网,找到CentOS7.4版本2、在CentOS7.4版本页面中,找到isos/3、进入页面后,可以看到x86_644、在CentOS下载页面中,选择CentOS-7-x86_64-DVD-170…

    2022年4月26日
    71
  • BMP文件解析_图片分析

    BMP文件解析_图片分析BMP文件简介BMP(全称Bitmap)是Window操作系统中的标准图像文件格式,可以分成两类:设备相关位图(DDB)和设备无关位图(DIB),使用非常广。它采用位映射存储格式,除了图像深度可选以外,不采用其他任何压缩,因此,BMP文件所占用的空间很大。BMP文件的图像深度可选lbit、4bit、8bit及24bit。BMP文件存储数据时,图像的扫描方式是按从左到右、从下到上的顺序。由于BMP文

    2025年6月16日
    3

发表回复

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

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