WebConfig操作帮助类

  ///
/// ConfigurationOperator 的摘要说明
///
public class ConfigurationOperator : IDisposable
{
private Configuration config;
public ConfigurationOperator()
: this(HttpContext.Current.Request.ApplicationPath)
{ }
public ConfigurationOperator(string path)
{
// Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
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();
}
} /// <summary>
/// 根据数据库连接串,测试是否连接成功
/// </summary>
/// <param name="connText"></param>
/// <returns></returns>
public string ResultText(string connText)
{
SqlConnection conn = new SqlConnection();
string txtValue = string.Empty; using (SqlConnection connection = new SqlConnection(connText))
{
try
{
connection.Open();
}
catch (Exception ex)
{
txtValue = ex.ToString();
}
return txtValue;
}
}
}

Web窗体.CS后台调用方法

        /// <summary>
/// 数据库连接串更新
/// </summary>
/// <param name="name">数据库连接串名称</param>
/// <param name="connStr">数据库连接(加密)</param>
public static void UpdateConnectionStrings(string name,string connStr)
{
//调用
ConfigurationOperator cfgOper = new ConfigurationOperator();
cfgOper.SetConnectionString(name, connStr);//添加一个节点,如果有就修改 cfgOper.Save();
cfgOper.Dispose();
} /// <summary>
/// 参数更新
/// </summary>
/// <param name="key">参数名称</param>
/// <param name="value">参数值</param>
public static void UpdateAppSettings(string key,string value)
{
//调用
ConfigurationOperator cfgOper = new ConfigurationOperator();
cfgOper.SetAppSetting(key, value);//添加一个节点,如果有就修改 cfgOper.Save();
cfgOper.Dispose();
}

参数获取

ConfigurationManager.AppSettings["canshu1"]

数据库链接获取

ConfigurationManager.ConnectionStrings["conStr"].ConnectionString

其他:

var config=  ConfigurationManager.AppSettings["minAmount"];//获取值
Configuration cfa = WebConfigurationManager.OpenWebConfiguration("~");
cfa.AppSettings.Settings.Add("key", "value");//添加值
cfa.AppSettings.Settings["redRule"].Value = "-1";//修改值
cfa.Save();//保存
//ConfigurationManager.RefreshSection("appSettings"); //强制刷新配置文件
05-18 08:28