C# ConfigHelper 辅助类介绍
发布时间:2023-11-20 21:53:09
//==============================================
// 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;
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 ex)
{
isSuccess = false;
throw ex;
}
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 ex)
{
isSuccess = false;
throw ex;
}
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 ex)
{
isSuccess = false;
}
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 ex)
{
isSuccess = false;
}
return isSuccess;
}
}


猜你喜欢
- 概述从今天开始, 小白我将带大家开启 Java 数据结构 & 算法的新篇章.字符串匹配字符串匹配 (String Matching)
- 在多线程编程中,最简单的方法,无非就是利用 AfxBeginThread 来创建一个工作线程,看一下这个函数的说明:CWinTh
- package com.example.myapi.email;import java.util.ArrayList;import java
- MyBatis全局配置文件MyBatis 的配置文件包含了影响 MyBatis 行为甚深的设置(settings)和属性(propertie
- 快速排序过程没有既不浪费空间又可以快一点的排序算法呢?那就是“快速排序”!光听这个名字是不是就觉得很高端呢。假设我们现在对“52 39 67
- 本文实例讲述了Java编程实现汉字按字母顺序排序的方法。分享给大家供大家参考,具体如下:String[] str0 = new String
- 一、简介编写手机App时,有时需要使用文字转语音(Text to Speech)的功能,比如开车时阅读收到的短信、导航语音提示、界面中比较重
- 本文实例讲述了C#实现图片切割的方法。分享给大家供大家参考,具体如下:图片切割就是把一幅大图片按用户要求切割成多幅小图片。dotnet环境下
- 本文实例为大家分享了java获取不同路径的方法,供大家参考,具体内容如下思路:自定义Button获取DialogManager、AudioM
- 一、说明到目前为止介绍的功能共享一对一的关系:即一个进程发送和一个进程接收。链接是通过标签建立的。本节介绍在多个进程中调用相同参数但执行不同
- 一:@@的意思是以@标注的字符出,其中所有的符号均为字符串符号,没有什么特殊字符,如''什么的,均默认为字符串
- 快速排序快速排序是一种比较高效的排序算法,采用“分而治之”的思想,通过多次比较和交换来实现排序,在一
- 面向对象编程(Object Oriented Programming)有三大特性:封装、继承、多态。在这里,和大家一起加深对三者的理解。封装
- 区块链是目前最热门的话题,广大读者都听说过比特币,或许还有智能合约,相信大家都非常想了解这一切是如何工作的。这篇文章就是帮助你使用 Java
- 本文实例讲述了Android下2d物理引擎Box2d用法。分享给大家供大家参考。具体如下:程序运行的时候需要加载Jbox2d的库,可到以下地
- 因工作需要,经常跟时间戳打交道,但是因为它仅仅是一个数字,我们很难直接看出它有什么意义,或两个时间戳之间究竟差了多长的间隔。于是从MSDN
- 前言在我们的日常企业应用开发当中,会碰到很多的图片素材访问的场景。比如社交类应用,您会在朋友圈中存放大量的图片,还有一些在线旅游或者直播的行
- 第一次接触到随机数还是在c语言里面 使用的是 rand(); 但是重新执行一次的时候会发现,诶,居然和上一次执行的结果是一样的,因为没有初始
- 对已有的apk文件进行重新打包,前面 Android签名机制:生成keystore、签名、查看签名信息 已经介绍了。本文介绍另外两种需求。使
- 循环结构分两大类,一类是当型,一类是直到型。当型:当布尔值表达式条件为True时,反复执行某语句,当布尔表达式的值为False时才停止循环,