你的位置:首页 > ASP.net教程

[ASP.net教程]两种读写配置文件的方案(app.config与web.config通用)


第一种方法:采用MS现有的ConfigurationManager来进行读写

using System.Configuration;namespace Zwj.TEMS.Common{  public abstract class ConfigHelper  {    private ConfigHelper()    { }    /// <summary>    /// 获取配置值    /// </summary>    /// <param name="key"></param>    /// <returns></returns>    public static string GetAppSettingValue(string key)    {      return ConfigurationManager.AppSettings[key];    }    /// <summary>    /// 设置配置值(存在则更新,不存在则新增)    /// </summary>    /// <param name="key"></param>    /// <param name="value"></param>    public static void SetAppSettingValue(string key, string value)    {      if (string.IsNullOrEmpty(GetAppSettingValue(key)))      {        ConfigurationManager.AppSettings.Add(key, value);      }      else      {        ConfigurationManager.AppSettings.Set(key, value);      }      Save();      ConfigurationManager.RefreshSection("appSettings");    }    /// <summary>    /// 删除配置值    /// </summary>    /// <param name="key"></param>    public static void RemoveAppSetting(string key)    {      ConfigurationManager.AppSettings.Remove(key);      Save();      ConfigurationManager.RefreshSection("appSettings");    }    /// <summary>    /// 获取连接字符串    /// </summary>    /// <param name="name"></param>    /// <returns></returns>    public static string GetConnectionString(string name)    {     return ConfigurationManager.ConnectionStrings[name].ConnectionString;    }    /// <summary>    /// 设置连接字符串的值(存在则更新,不存在则新增)    /// </summary>    /// <param name="name"></param>    /// <param name="connstr"></param>    /// <param name="provider"></param>    public static void SetConnectionString(string name,string connstr, string provider)    {      ConnectionStringSettings connStrSettings =ConfigurationManager.ConnectionStrings[name];       new ConnectionStringSettings(name, connstr, provider);      if (connStrSettings != null)      {        connStrSettings.ConnectionString = connstr;        connStrSettings.ProviderName = provider;      }      else      {        ConfigurationManager.ConnectionStrings.Add(connStrSettings);      }      Save();      ConfigurationManager.RefreshSection("connectionStrings");    }    /// <summary>    /// 删除连接字符串配置项    /// </summary>    /// <param name="name"></param>    public static void RemoveConnectionString(string name)    {      ConfigurationManager.ConnectionStrings.Remove(name);      Save();      ConfigurationManager.RefreshSection("connectionStrings");    }    private static void Save()    {      var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);      config.Save(ConfigurationSaveMode.Modified);    }  }}

第二种方法:采用原生的

//==============================================// FileName: ConfigManager// Description: 静态方法业务类,用于对C#、ASP.NET中的WinForm & WebForm 项目程序配置文件// app.config和web.config的[appSettings]和[connectionStrings]节点进行新增、修改、删除和读取相关的操作。//==============================================using System;using System.Data;using System.Configuration;using System.Web;using System.Collections.Generic;using System.Text;using System.

更多IT相关技术文章与资讯,欢迎光临我的个人网站:http://www.zuowenjun.cn