C# ConfigHelper 輔助類介紹
//==============================================
// FileName: ConfigManager
// Description: 靜態(tài)方法業(yè)務(wù)類,用于對(duì)C#、ASP.NET中的WinForm & WebForm 項(xiàng)目程序配置文件
// app.config和web.config的[appSettings]和[connectionStrings]節(jié)點(diǎn)進(jìn)行新增、修改、刪除和讀取相關(guān)的操作。
//==============================================
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Xml;
public enum ConfigurationFile
{
AppConfig=1,
WebConfig=2
}
/// <summary>
/// ConfigManager 應(yīng)用程序配置文件管理器
/// </summary>
public class ConfigManager
{
public ConfigManager()
{
//
// TODO: 在此處添加構(gòu)造函數(shù)邏輯
//
}
/// <summary>
/// 對(duì)[appSettings]節(jié)點(diǎn)依據(jù)Key值讀取到Value值,返回字符串
/// </summary>
/// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
/// <param name="key">要讀取的Key值</param>
/// <returns>返回Value值的字符串</returns>
public static string ReadValueByKey(ConfigurationFile configurationFile, string key)
{
string value = string.Empty;
string filename = string.Empty;
if (configurationFile.ToString()==ConfigurationFile.AppConfig.ToString())
{
filename = System.Windows.Forms.Application.ExecutablePath + ".config";
}
else
{
filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config";
}
XmlDocument doc = new XmlDocument();
doc.Load(filename); //加載配置文件
XmlNode node = doc.SelectSingleNode("http://appSettings"); //得到[appSettings]節(jié)點(diǎn)
////得到[appSettings]節(jié)點(diǎn)中關(guān)于Key的子節(jié)點(diǎn)
XmlElement element = (XmlElement)node.SelectSingleNode("http://add[@key='" + key + "']");
if (element != null)
{
value = element.GetAttribute("value");
}
return value;
}
/// <summary>
/// 對(duì)[connectionStrings]節(jié)點(diǎn)依據(jù)name值讀取到connectionString值,返回字符串
/// </summary>
/// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
/// <param name="name">要讀取的name值</param>
/// <returns>返回connectionString值的字符串</returns>
public static string ReadConnectionStringByName(ConfigurationFile configurationFile, string name)
{
string connectionString = string.Empty;
string filename = string.Empty;
if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString())
{
filename = System.Windows.Forms.Application.ExecutablePath + ".config";
}
else
{
filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config";
}
XmlDocument doc = new XmlDocument();
doc.Load(filename); //加載配置文件
XmlNode node = doc.SelectSingleNode("http://connectionStrings"); //得到[appSettings]節(jié)點(diǎn)
////得到[connectionString]節(jié)點(diǎn)中關(guān)于name的子節(jié)點(diǎn)
XmlElement element = (XmlElement)node.SelectSingleNode("http://add[@name='" + name + "']");
if (element != null)
{
connectionString = element.GetAttribute("connectionString");
}
return connectionString;
}
/// <summary>
/// 更新或新增[appSettings]節(jié)點(diǎn)的子節(jié)點(diǎn)值,存在則更新子節(jié)點(diǎn)Value,不存在則新增子節(jié)點(diǎn),返回成功與否布爾值
/// </summary>
/// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
/// <param name="key">子節(jié)點(diǎn)Key值</param>
/// <param name="value">子節(jié)點(diǎn)value值</param>
/// <returns>返回成功與否布爾值</returns>
public static bool UpdateOrCreateAppSetting(ConfigurationFile configurationFile, string key, string value)
{
bool isSuccess = false;
string filename = string.Empty;
if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString())
{
filename = System.Windows.Forms.Application.ExecutablePath + ".config";
}
else
{
filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config";
}
XmlDocument doc = new XmlDocument();
doc.Load(filename); //加載配置文件
XmlNode node = doc.SelectSingleNode("http://appSettings"); //得到[appSettings]節(jié)點(diǎn)
try
{
////得到[appSettings]節(jié)點(diǎn)中關(guān)于Key的子節(jié)點(diǎn)
XmlElement element = (XmlElement)node.SelectSingleNode("http://add[@key='" + key + "']");
if (element != null)
{
//存在則更新子節(jié)點(diǎn)Value
element.SetAttribute("value", value);
}
else
{
//不存在則新增子節(jié)點(diǎn)
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("key", key);
subElement.SetAttribute("value", value);
node.AppendChild(subElement);
}
//保存至配置文件(方式一)
using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
{
xmlwriter.Formatting = Formatting.Indented;
doc.WriteTo(xmlwriter);
xmlwriter.Flush();
}
isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
throw ex;
}
return isSuccess;
}
/// <summary>
/// 更新或新增[connectionStrings]節(jié)點(diǎn)的子節(jié)點(diǎn)值,存在則更新子節(jié)點(diǎn),不存在則新增子節(jié)點(diǎn),返回成功與否布爾值
/// </summary>
/// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
/// <param name="name">子節(jié)點(diǎn)name值</param>
/// <param name="connectionString">子節(jié)點(diǎn)connectionString值</param>
/// <param name="providerName">子節(jié)點(diǎn)providerName值</param>
/// <returns>返回成功與否布爾值</returns>
public static bool UpdateOrCreateConnectionString(ConfigurationFile configurationFile, string name, string connectionString, string providerName)
{
bool isSuccess = false;
string filename = string.Empty;
if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString())
{
filename = System.Windows.Forms.Application.ExecutablePath + ".config";
}
else
{
filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config";
}
XmlDocument doc = new XmlDocument();
doc.Load(filename); //加載配置文件
XmlNode node = doc.SelectSingleNode("http://connectionStrings"); //得到[connectionStrings]節(jié)點(diǎn)
try
{
////得到[connectionStrings]節(jié)點(diǎn)中關(guān)于Name的子節(jié)點(diǎn)
XmlElement element = (XmlElement)node.SelectSingleNode("http://add[@name='" + name + "']");
if (element != null)
{
//存在則更新子節(jié)點(diǎn)
element.SetAttribute("connectionString", connectionString);
element.SetAttribute("providerName", providerName);
}
else
{
//不存在則新增子節(jié)點(diǎn)
XmlElement subElement = doc.CreateElement("add");
subElement.SetAttribute("name", name);
subElement.SetAttribute("connectionString", connectionString);
subElement.SetAttribute("providerName", providerName);
node.AppendChild(subElement);
}
//保存至配置文件(方式二)
doc.Save(filename);
isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
throw ex;
}
return isSuccess;
}
/// <summary>
/// 刪除[appSettings]節(jié)點(diǎn)中包含Key值的子節(jié)點(diǎn),返回成功與否布爾值
/// </summary>
/// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
/// <param name="key">要?jiǎng)h除的子節(jié)點(diǎn)Key值</param>
/// <returns>返回成功與否布爾值</returns>
public static bool DeleteByKey(ConfigurationFile configurationFile, string key)
{
bool isSuccess = false;
string filename = string.Empty;
if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString())
{
filename = System.Windows.Forms.Application.ExecutablePath + ".config";
}
else
{
filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config";
}
XmlDocument doc = new XmlDocument();
doc.Load(filename); //加載配置文件
XmlNode node = doc.SelectSingleNode("http://appSettings"); //得到[appSettings]節(jié)點(diǎn)
////得到[appSettings]節(jié)點(diǎn)中關(guān)于Key的子節(jié)點(diǎn)
XmlElement element = (XmlElement)node.SelectSingleNode("http://add[@key='" + key + "']");
if (element != null)
{
//存在則刪除子節(jié)點(diǎn)
element.ParentNode.RemoveChild(element);
}
else
{
//不存在
}
try
{
//保存至配置文件(方式一)
using (XmlTextWriter xmlwriter = new XmlTextWriter(filename, null))
{
xmlwriter.Formatting = Formatting.Indented;
doc.WriteTo(xmlwriter);
xmlwriter.Flush();
}
isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
}
return isSuccess;
}
/// <summary>
/// 刪除[connectionStrings]節(jié)點(diǎn)中包含name值的子節(jié)點(diǎn),返回成功與否布爾值
/// </summary>
/// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
/// <param name="name">要?jiǎng)h除的子節(jié)點(diǎn)name值</param>
/// <returns>返回成功與否布爾值</returns>
public static bool DeleteByName(ConfigurationFile configurationFile, string name)
{
bool isSuccess = false;
string filename = string.Empty;
if (configurationFile.ToString() == ConfigurationFile.AppConfig.ToString())
{
filename = System.Windows.Forms.Application.ExecutablePath + ".config";
}
else
{
filename = System.AppDomain.CurrentDomain.BaseDirectory + "web.config";
}
XmlDocument doc = new XmlDocument();
doc.Load(filename); //加載配置文件
XmlNode node = doc.SelectSingleNode("http://connectionStrings"); //得到[connectionStrings]節(jié)點(diǎn)
////得到[connectionStrings]節(jié)點(diǎn)中關(guān)于Name的子節(jié)點(diǎn)
XmlElement element = (XmlElement)node.SelectSingleNode("http://add[@name='" + name + "']");
if (element != null)
{
//存在則刪除子節(jié)點(diǎn)
node.RemoveChild(element);
}
else
{
//不存在
}
try
{
//保存至配置文件(方式二)
doc.Save(filename);
isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
}
return isSuccess;
}
}
相關(guān)文章
C#在DataTable中根據(jù)條件刪除某一行的實(shí)現(xiàn)方法
我們通常的方法是把數(shù)據(jù)源放在DataTable里面,但是偶爾也會(huì)需要把不要的行移除,怎么實(shí)現(xiàn)呢,下面通過代碼給大家介紹c# atatable 刪除行的方法,需要的朋友一起看下吧2016-05-05C#中WebBrowser.DocumentCompleted事件多次調(diào)用問題解決方法
這篇文章主要介紹了C#中WebBrowser.DocumentCompleted事件多次調(diào)用問題解決方法,本文講解了3種情況和各自情況的解決方法,需要的朋友可以參考下2015-01-01C#數(shù)組學(xué)習(xí)相關(guān)資料整理
最近開始學(xué)習(xí)c#,并有幸接觸到了數(shù)組方便的操作,感覺確實(shí)不錯(cuò),這里簡單的整理下c#相關(guān)的學(xué)習(xí)資料,方便大家學(xué)習(xí)2012-09-09C# 復(fù)制指定節(jié)點(diǎn)的所有子孫節(jié)點(diǎn)到新建的節(jié)點(diǎn)下
這篇文章主要介紹了C# 復(fù)制指定節(jié)點(diǎn)的所有子孫節(jié)點(diǎn)到新建的節(jié)點(diǎn)下的相關(guān)資料,非常不錯(cuò)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10使用C#獲取遠(yuǎn)程圖片 Form用戶名與密碼Authorization認(rèn)證的實(shí)現(xiàn)
本篇文章介紹了,使用C#獲取遠(yuǎn)程圖片 Form用戶名與密碼Authorization認(rèn)證的實(shí)現(xiàn)。需要的朋友參考下2013-04-04