码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET Web.Config 读写辅助类

时间:2014-09-12 16:39:23      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:http   io   os   使用   ar   数据   sp   cti   on   

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

namespace Whir.ezEIP
{
    /// <summary>
    /// WebConfig读写辅助类
    /// </summary>
    public class WebConfigManager : IDisposable
    {
        private Configuration _config;

        /// <summary>
        /// WebConfig读写辅助类
        /// </summary>
        public WebConfigManager()
            : this(HttpContext.Current.Request.ApplicationPath)
        {
        }
        /// <summary>
        /// WebConfig读写辅助类
        /// </summary>
        /// <param name="path"></param>
        public WebConfigManager(string path)
        {
            _config = WebConfigurationManager.OpenWebConfiguration(path);
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (_config != null)
            {
                _config.Save();
            }
        }

        #endregion

        /// <summary> 
        /// 设置应用程序配置节点,如果已经存在此节点,则会修改该节点的值,否则添加此节点
        /// </summary> 
        /// <param name="key">节点名称</param> 
        /// <param name="value">节点值</param> 
        public void SetAppSetting(string key, string value)
        {
            var appSetting = (AppSettingsSection)_config.GetSection("appSettings");
            if (appSetting.Settings[key] == null) //如果不存在此节点,则添加 
            {
                appSetting.Settings.Add(key, value);
            }
            else //如果存在此节点,则修改 
            {
                appSetting.Settings[key].Value = value;
            }
            Save();
        }

        /// <summary> 
        /// 设置数据库连接字符串节点,如果不存在此节点,则会添加此节点及对应的值,存在则修改 
        /// </summary> 
        /// <param name="key">节点名称</param>
        /// <param name="connectionString"> </param>
        public void SetConnectionString(string key, string connectionString)
        {
            var connectionSetting = (ConnectionStringsSection)_config.GetSection("connectionStrings");
            if (connectionSetting.ConnectionStrings[key] == null) //如果不存在此节点,则添加 
            {
                var connectionStringSettings = new ConnectionStringSettings(key, connectionString);
                connectionSetting.ConnectionStrings.Add(connectionStringSettings);
            }
            else //如果存在此节点,则修改 
            {
                connectionSetting.ConnectionStrings[key].ConnectionString = connectionString;
            }
            Save();
        }

        /// <summary> 
        /// 保存所作的修改 
        /// </summary> 
        public void Save()
        {
            _config.Save();
            _config = null;
        }
    }
}

 

使用方法:

   var config = new WebConfigManager();
   config.SetAppSetting("StrRegex", "");

 

读取AppSetting和ConnectionString:

string fileType=ConfigurationManager.AppSettings["FileType "];

string connectionString = ConfigurationManager.ConnectionStrings["AspNetStudyConnectionString1"].ConnectionString;

 

ASP.NET Web.Config 读写辅助类

标签:http   io   os   使用   ar   数据   sp   cti   on   

原文地址:http://www.cnblogs.com/feng521/p/3968402.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!