C#操作配置文件中appSettings,connectionStrings节点
???
using System;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
namespace myConfiguration
{
??? #region 配置信息的操作類
??? /// <summary>
??? /// 配置信息的操作
??? /// </summary>
??? public class ConfigurationOperator:IDisposable
??? {
??????? #region 變量的聲明
??????? /// <summary>
??????? /// Configuration Object
??????? /// </summary>
??????? private Configuration config;
??????? #endregion
??????? #region 構造函數,有參數(當前應用程序的虛擬路徑)
??????? /// <summary>
??????? /// 構造函數,有參數(當前應用程序的虛擬路徑)
??????? /// </summary>
??????? public ConfigurationOperator()
??????????? : this(HttpContext.Current.Request.ApplicationPath)
??????? {
??????? }
??????? #endregion
??????? #region 構造函數,有參數(其他應用程序的虛擬路徑)
??????? /// <summary>
??????? /// 構造函數,有參數(其他應用程序的虛擬路徑)
??????? /// </summary>
??????? /// <param name="path">其他應用程序的虛擬路徑</param>
??????? public ConfigurationOperator(string path)
??????? {
??????????? config = WebConfigurationManager.OpenWebConfiguration(path);
??????? }
??????? #endregion
??????? #region 獲取當前或其他應用程序配置文件中appSettings的所有keyName方法
??????? /// <summary>
??????? /// 定義獲取當前或其他應用程序appSettings的所有keyName方法
??????? /// </summary>
??????? /// <returns>返回appSettings的所有keyName</returns>
??????? public string[] ActiveALLAppSettingsSection()
??????? {
??????????? AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
??????????? string[] appKeys = appSettings.Settings.AllKeys;
??????????? return appKeys;
??????? }
??????? #endregion
??????? #region 設置當前或者其他應用程序配置文件中的appSettings節點
??????? /// <summary>
??????? /// 定義設置當前或者其他應用程序配置文件中的appSettings節點
??????? /// </summary>
??????? /// <param name="key">keyName</param>
??????? /// <param name="value">keyValue</param>
??????? public void SetAppSettingsSection(string key,string value)
??????? {
??????????? AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
??????????? if (appSettings.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
}
總結
以上是生活随笔為你收集整理的C#操作配置文件中appSettings,connectionStrings节点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 评论:电商巨头们谁有勇气晒晒“价格战”账
- 下一篇: 小窍门解决大问题(组图)