用户系统;
using System.Configuration;
using系统. web;
using System.Web.Configuration;
名称空间我的配置
{
#region配置信息的操作类
///summary
//使用配置信息
////summary
publicclassconfigurationoperator : idisposable
{
#region变量的声明
///summary
///配置对象
////summary
私人配置配置配置;
#endregion
#region构造函数,有自变量(当前APP应用程序的虚拟路径)
///summary
//构造函数,有自变量(当前APP应用程序的虚拟路径) )。
////summary
公共配置操作器(
3360 this (httpcontext.current.request.application path ) ) ) ) ) )。
{
}
#endregion
#region构造函数,有自变量(其他APP应用程序的虚拟路径) ) ) ) ) ) ) ) )。
///summary
//构造函数,有自变量(其他APP应用程序的虚拟路径) )。
////summary
///param name=’path ‘指向其他APP应用程序的虚拟路径/param
公共配置操作器(字符串路径)。
{
config=webconfigurationmanager.openwebconfiguration (path );
}
#endregion
#region获取当前或其他APP应用程序配置文件中appSettings的所有keyName方法
///summary
//定义获取当前或其他APP应用程序appSettings的所有keyName方法
////summary
///returns返回应用程序的所有keyName/returns
公共字符串[ ] activeallappsettingssection (
{
appsettingssectionappsettings=(appsettings section ) config.getsection(‘appsettings ‘ );
string [ ] appkeys=appsettings.settings.all keys;
返回应用程序;
}
#endregion
#region设置当前或其他APP应用程序配置文件的应用程序节点
///summary
//定义当前或其他APP应用程序配置文件的应用程序节点
////summary
///param name=’ key ‘ keyname/param
//param name=’ value ‘ key value/param
公共语音设置(字符串密钥,字符串值) ) ) )。
{
appsettingssectionappsettings=(appsettings section ) config.getsection(‘appsettings ‘ );
if (应用
tings.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
}