Fork me on GitHub

Net操作配置文件(Web.config|App.config)通用类

一、Net操作web.config通用类(代码如下):

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


/// ConfigurationOperator 的摘要说明
public class ConfigurationOperator : IDisposable
{
    private Configuration config;
    public ConfigurationOperator()
        : this(HttpContext.Current.Request.ApplicationPath) { }
    public ConfigurationOperator(string path)
    {
        config = WebConfigurationManager.OpenWebConfiguration(path);
    }

    /// 设置应用程序配置节点,如果已经存在此节点,则会修改该节点的值,否则添加此节点
    ///

    /// 节点名称 
    /// 节点值 
    public void SetAppSetting(string key, string value)
    {
        AppSettingsSection appSetting = (AppSettingsSection)config.GetSection("appSettings");
        if (appSetting.Settings[key] == null)//如果不存在此节点,则添加
        {
            appSetting.Settings.Add(key, value);
        }
        else//如果存在此节点,则修改
        {
            appSetting.Settings[key].Value = value;
        }
    }
    ///

    /// 设置数据库连接字符串节点,如果不存在此节点,则会添加此节点及对应的值,存在则修改
    ///

    /// 节点名称 
    /// 节点值 
    public void SetConnectionString(string key, string connectionString)
    {
        ConnectionStringsSection connectionSetting = (ConnectionStringsSection)config.GetSection("connectionStrings");
        if (connectionSetting.ConnectionStrings[key] == null)//如果不存在此节点,则添加
        {
            ConnectionStringSettings connectionStringSettings = new ConnectionStringSettings(key, connectionString);
            connectionSetting.ConnectionStrings.Add(connectionStringSettings);
        }
        else//如果存在此节点,则修改
        {
            connectionSetting.ConnectionStrings[key].ConnectionString = connectionString;
        }
    }
    ///

    /// 保存所作的修改
    public void Save()
    {
        config.Save();
        config = null;
    }
    public void Dispose()
    {
        if (config != null)
        {
            config.Save();
        }
    }
}

 

二、调用方法:

// 连接字符串
string connectionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}; max pool size=150",
    tbx_ip.Text.Trim(), tbx_dbname.Text.Trim(), tbx_username.Text.Trim(), tbx_pwd.Text.Trim());

ConfigurationOperator config = new ConfigurationOperator();
config.SetConnectionString("defaultConnection", connectionString);
config.Save();
posted @ 2012-07-26 15:48  磊哥|www.javacn.site  阅读(1002)  评论(0编辑  收藏  举报