C#对App.config文件或者web.config文件中节点的操作类
生活随笔
收集整理的這篇文章主要介紹了
C#对App.config文件或者web.config文件中节点的操作类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
//==============================================
//??????? 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.Xml;
using TempusMq;
namespace Kevin.DAL
{
?? public enum ConfigurationFile
?? {
??????? AppConfig=1,
??????? WebConfig=2
?? }
??? /// <summary>
??? /// ConfigManager 應用程序配置文件管理器
??? /// </summary>
?? public class ConfigManager
?? {
?????? public ConfigManager()
?????? {
?????????? //
?????????? // TODO: 在此處添加構造函數邏輯
?????????? //
?????? }
?????? /// <summary>
?????? /// 對[appSettings]節點依據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("//appSettings");?? //得到[appSettings]節點
?????????? 得到[appSettings]節點中關于Key的子節點
?????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
?????????? if (element != null)
?????????? {
?????????????? value = element.GetAttribute("value");
?????????? }
?????????? return value;
?????? }
?????? /// <summary>
?????? /// 對[connectionStrings]節點依據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("//connectionStrings");?? //得到[appSettings]節點
?????????? 得到[connectionString]節點中關于name的子節點
?????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
?????????? if (element != null)
?????????? {
?????????????? connectionString = element.GetAttribute("connectionString");
?????????? }
?????????? return connectionString;
?????? }
?????? /// <summary>
?????? /// 更新或新增[appSettings]節點的子節點值,存在則更新子節點Value,不存在則新增子節點,返回成功與否布爾值
?????? /// </summary>
?????? /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
?????? /// <param name="key">子節點Key值</param>
?????? /// <param name="value">子節點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("//appSettings");?? //得到[appSettings]節點
?????????? try
?????????? {
?????????????? 得到[appSettings]節點中關于Key的子節點
?????????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
?????????????? if (element != null)
?????????????? {
?????????????????? //存在則更新子節點Value
?????????????????? element.SetAttribute("value", value);
?????????????? }
?????????????? else
?????????????? {
?????????????????? //不存在則新增子節點
?????????????????? 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 e)
?????????? {
?????????????? isSuccess = false;
?????????????? //輸出的調試字符串
?????????????? string strOuput = string.Format("更新或新增[appSettings]節點的子節點值失敗:{0}\n", e.Message);
?????????????? //將信息寫入到日志輸出文件
?????????????? DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
????????????? ?
?????????? }
?????????? return isSuccess;
?????? }
?????? /// <summary>
?????? /// 更新或新增[appSettings]節點的子節點值,存在則更新子節點Value,不存在則新增子節點,返回成功與否布爾值
?????? /// </summary>
?????? /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
?????? /// <param name="key">子節點Key值</param>
?????? /// <param name="value">子節點value值</param>
?????? /// <returns>返回成功與否布爾值</returns>
?????? public static bool UpdateOrCreateAppSetting(ConfigurationFile configurationFile,string configFileName,string key, string value)
?????????? {
?????????????? bool isSuccess = false;
?????????????? string filename = configFileName;
?????????????? XmlDocument doc = new XmlDocument();
?????????????? doc.Load(filename); //加載配置文件
?????????????? XmlNode node = doc.SelectSingleNode("//appSettings");?? //得到[appSettings]節點
?????????????? try
??????????????? {
??????????????????? 得到[appSettings]節點中關于Key的子節點
??????????????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
??????????????????? if (element != null)
??????????????????????? {
??????????????????????? //存在則更新子節點Value
??????????????????????? element.SetAttribute("value", value);
??????????????????????? }
??????????????????? else
??????????????????????? {
??????????????????????? //不存在則新增子節點
??????????????????????? 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 e)
??????????????? {
??????????????? isSuccess = false;
??????????????? //輸出的調試字符串
??????????????? string strOuput = string.Format("更新或新增[appSettings]節點的子節點值失敗:{0}\n", e.Message);
??????????????? //將信息寫入到日志輸出文件
??????????????? DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
??????????????? }
?????????????? return isSuccess;
?????????? }
?????? /// <summary>
?????? /// 更新或新增[connectionStrings]節點的子節點值,存在則更新子節點,不存在則新增子節點,返回成功與否布爾值
?????? /// </summary>
?????? /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
?????? /// <param name="name">子節點name值</param>
?????? /// <param name="connectionString">子節點connectionString值</param>
?????? /// <param name="providerName">子節點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("//connectionStrings");?? //得到[connectionStrings]節點
?????????? try
?????????? {
?????????????? 得到[connectionStrings]節點中關于Name的子節點
?????????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
?????????????? if (element != null)
?????????????? {
?????????????????? //存在則更新子節點
?????????????????? element.SetAttribute("connectionString", connectionString);
?????????????????? element.SetAttribute("providerName", providerName);
?????????????? }
?????????????? else
?????????????? {
?????????????????? //不存在則新增子節點
?????????????????? 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 e)
?????????? {
?????????????? isSuccess = false;
?????????????? //輸出的調試字符串
?????????????? string strOuput = string.Format("更新或新增[connectionStrings]節點的子節點值:{0}失敗:{1}\n",name,e.Message);
?????????????? //將信息寫入到日志輸出文件
?????????????? DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
?????????? }
?????????? return isSuccess;
?????? }
?????? /// <summary>
?????? /// 刪除[appSettings]節點中包含Key值的子節點,返回成功與否布爾值
?????? /// </summary>
?????? /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
?????? /// <param name="key">要刪除的子節點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("//appSettings");?? //得到[appSettings]節點
?????????? 得到[appSettings]節點中關于Key的子節點
?????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
?????????? if (element != null)
?????????? {
?????????????? //存在則刪除子節點
?????????????? 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 e)
?????????? {
?????????????? //輸出的調試字符串
?????????????? string strOuput = string.Format("刪除[appSettings]節點中包含Key:{0}值的子節點失敗:{1}\n",key, e.Message);
?????????????? //將信息寫入到日志輸出文件
?????????????? DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
?????????? }
?????????? return isSuccess;
?????? }
?????? /// <summary>
?????? /// 刪除[connectionStrings]節點中包含name值的子節點,返回成功與否布爾值
?????? /// </summary>
?????? /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
?????? /// <param name="name">要刪除的子節點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("//connectionStrings");?? //得到[connectionStrings]節點
?????????? 得到[connectionStrings]節點中關于Name的子節點
?????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
?????????? if (element != null)
?????????? {
?????????????? //存在則刪除子節點
?????????????? node.RemoveChild(element);
?????????? }
?????????? else
?????????? {
?????????????? //不存在
?????????? }
?????????? try
?????????? {
?????????????? //保存至配置文件(方式二)
?????????????? doc.Save(filename);
?????????????? isSuccess = true;
?????????? }
?????????? catch (Exception e)
?????????? {
?????????????? isSuccess = false;
?????????????? //輸出的調試字符串
?????????????? string strOuput = string.Format("刪除[connectionStrings]節點中包含name:{0}值的子節點失敗:{1}\n",name,e.Message);
?????????????? //將信息寫入到日志輸出文件
?????????????? DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
?????????? }
?????????? return isSuccess;
?????? }
?? }
}
//??????? 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.Xml;
using TempusMq;
namespace Kevin.DAL
{
?? public enum ConfigurationFile
?? {
??????? AppConfig=1,
??????? WebConfig=2
?? }
??? /// <summary>
??? /// ConfigManager 應用程序配置文件管理器
??? /// </summary>
?? public class ConfigManager
?? {
?????? public ConfigManager()
?????? {
?????????? //
?????????? // TODO: 在此處添加構造函數邏輯
?????????? //
?????? }
?????? /// <summary>
?????? /// 對[appSettings]節點依據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("//appSettings");?? //得到[appSettings]節點
?????????? 得到[appSettings]節點中關于Key的子節點
?????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
?????????? if (element != null)
?????????? {
?????????????? value = element.GetAttribute("value");
?????????? }
?????????? return value;
?????? }
?????? /// <summary>
?????? /// 對[connectionStrings]節點依據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("//connectionStrings");?? //得到[appSettings]節點
?????????? 得到[connectionString]節點中關于name的子節點
?????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
?????????? if (element != null)
?????????? {
?????????????? connectionString = element.GetAttribute("connectionString");
?????????? }
?????????? return connectionString;
?????? }
?????? /// <summary>
?????? /// 更新或新增[appSettings]節點的子節點值,存在則更新子節點Value,不存在則新增子節點,返回成功與否布爾值
?????? /// </summary>
?????? /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
?????? /// <param name="key">子節點Key值</param>
?????? /// <param name="value">子節點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("//appSettings");?? //得到[appSettings]節點
?????????? try
?????????? {
?????????????? 得到[appSettings]節點中關于Key的子節點
?????????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
?????????????? if (element != null)
?????????????? {
?????????????????? //存在則更新子節點Value
?????????????????? element.SetAttribute("value", value);
?????????????? }
?????????????? else
?????????????? {
?????????????????? //不存在則新增子節點
?????????????????? 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 e)
?????????? {
?????????????? isSuccess = false;
?????????????? //輸出的調試字符串
?????????????? string strOuput = string.Format("更新或新增[appSettings]節點的子節點值失敗:{0}\n", e.Message);
?????????????? //將信息寫入到日志輸出文件
?????????????? DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
????????????? ?
?????????? }
?????????? return isSuccess;
?????? }
?????? /// <summary>
?????? /// 更新或新增[appSettings]節點的子節點值,存在則更新子節點Value,不存在則新增子節點,返回成功與否布爾值
?????? /// </summary>
?????? /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
?????? /// <param name="key">子節點Key值</param>
?????? /// <param name="value">子節點value值</param>
?????? /// <returns>返回成功與否布爾值</returns>
?????? public static bool UpdateOrCreateAppSetting(ConfigurationFile configurationFile,string configFileName,string key, string value)
?????????? {
?????????????? bool isSuccess = false;
?????????????? string filename = configFileName;
?????????????? XmlDocument doc = new XmlDocument();
?????????????? doc.Load(filename); //加載配置文件
?????????????? XmlNode node = doc.SelectSingleNode("//appSettings");?? //得到[appSettings]節點
?????????????? try
??????????????? {
??????????????????? 得到[appSettings]節點中關于Key的子節點
??????????????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
??????????????????? if (element != null)
??????????????????????? {
??????????????????????? //存在則更新子節點Value
??????????????????????? element.SetAttribute("value", value);
??????????????????????? }
??????????????????? else
??????????????????????? {
??????????????????????? //不存在則新增子節點
??????????????????????? 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 e)
??????????????? {
??????????????? isSuccess = false;
??????????????? //輸出的調試字符串
??????????????? string strOuput = string.Format("更新或新增[appSettings]節點的子節點值失敗:{0}\n", e.Message);
??????????????? //將信息寫入到日志輸出文件
??????????????? DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
??????????????? }
?????????????? return isSuccess;
?????????? }
?????? /// <summary>
?????? /// 更新或新增[connectionStrings]節點的子節點值,存在則更新子節點,不存在則新增子節點,返回成功與否布爾值
?????? /// </summary>
?????? /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
?????? /// <param name="name">子節點name值</param>
?????? /// <param name="connectionString">子節點connectionString值</param>
?????? /// <param name="providerName">子節點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("//connectionStrings");?? //得到[connectionStrings]節點
?????????? try
?????????? {
?????????????? 得到[connectionStrings]節點中關于Name的子節點
?????????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
?????????????? if (element != null)
?????????????? {
?????????????????? //存在則更新子節點
?????????????????? element.SetAttribute("connectionString", connectionString);
?????????????????? element.SetAttribute("providerName", providerName);
?????????????? }
?????????????? else
?????????????? {
?????????????????? //不存在則新增子節點
?????????????????? 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 e)
?????????? {
?????????????? isSuccess = false;
?????????????? //輸出的調試字符串
?????????????? string strOuput = string.Format("更新或新增[connectionStrings]節點的子節點值:{0}失敗:{1}\n",name,e.Message);
?????????????? //將信息寫入到日志輸出文件
?????????????? DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
?????????? }
?????????? return isSuccess;
?????? }
?????? /// <summary>
?????? /// 刪除[appSettings]節點中包含Key值的子節點,返回成功與否布爾值
?????? /// </summary>
?????? /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
?????? /// <param name="key">要刪除的子節點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("//appSettings");?? //得到[appSettings]節點
?????????? 得到[appSettings]節點中關于Key的子節點
?????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
?????????? if (element != null)
?????????? {
?????????????? //存在則刪除子節點
?????????????? 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 e)
?????????? {
?????????????? //輸出的調試字符串
?????????????? string strOuput = string.Format("刪除[appSettings]節點中包含Key:{0}值的子節點失敗:{1}\n",key, e.Message);
?????????????? //將信息寫入到日志輸出文件
?????????????? DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
?????????? }
?????????? return isSuccess;
?????? }
?????? /// <summary>
?????? /// 刪除[connectionStrings]節點中包含name值的子節點,返回成功與否布爾值
?????? /// </summary>
?????? /// <param name="configurationFile">要操作的配置文件名稱,枚舉常量</param>
?????? /// <param name="name">要刪除的子節點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("//connectionStrings");?? //得到[connectionStrings]節點
?????????? 得到[connectionStrings]節點中關于Name的子節點
?????????? XmlElement element = (XmlElement)node.SelectSingleNode("//add[@name='" + name + "']");
?????????? if (element != null)
?????????? {
?????????????? //存在則刪除子節點
?????????????? node.RemoveChild(element);
?????????? }
?????????? else
?????????? {
?????????????? //不存在
?????????? }
?????????? try
?????????? {
?????????????? //保存至配置文件(方式二)
?????????????? doc.Save(filename);
?????????????? isSuccess = true;
?????????? }
?????????? catch (Exception e)
?????????? {
?????????????? isSuccess = false;
?????????????? //輸出的調試字符串
?????????????? string strOuput = string.Format("刪除[connectionStrings]節點中包含name:{0}值的子節點失敗:{1}\n",name,e.Message);
?????????????? //將信息寫入到日志輸出文件
?????????????? DllComm.TP_WriteAppLogFileEx(DllComm.g_AppLogFileName, strOuput);
?????????? }
?????????? return isSuccess;
?????? }
?? }
}
轉載于:https://www.cnblogs.com/kevinGao/archive/2011/11/01/2233424.html
總結
以上是生活随笔為你收集整理的C#对App.config文件或者web.config文件中节点的操作类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用手机控制树莓派
- 下一篇: Dubbo的核心玩法三