用于获取或设置Web.config/*.exe.config中节点数据的辅助类
1. 用于获取或设置Web.config/*.exe.config中节点数据的辅助类
/**//// <summary>
/// 用于获取或设置Web.config/*.exe.config中节点数据的辅助类
/// </summary>
public sealed class AppConfig
{
private string filePath;
/**//// <summary>
/// 从当前目录中按顺序检索Web.Config和*.App.Config文件。
/// 如果找到一个,则使用它作为配置文件;否则会抛出一个ArgumentNullException异常。
/// </summary>
public AppConfig()
{
string webconfig = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Web.Config");
string appConfig = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.Replace(".vshost", "");
if (File.Exists(webconfig))
{
filePath = webconfig;
}
else if (File.Exists(appConfig))
{
filePath = appConfig;
}
else
{
throw new ArgumentNullException("没有找到Web.Config文件或者应用程序配置文件, 请指定配置文件");
}
}
/**//// <summary>
/// 用户指定具体的配置文件路径
/// </summary>
/// <param name="configFilePath">配置文件路径(绝对路径)</param>
public AppConfig(string configFilePath)
{
filePath = configFilePath;
}
/**//// <summary>
/// 设置程序的config文件
/// </summary>
/// <param name="keyName">键名</param>
/// <param name="keyValue">键值</param>
public void AppConfigSet(string keyName, string keyValue)
{
//由于存在多个Add键值,使得访问appSetting的操作不成功,故注释下面语句,改用新的方式
/**//*
string xpath = "//add[@key=‘" + keyName + "‘]";
XmlDocument document = new XmlDocument();
document.Load(filePath);
XmlNode node = document.SelectSingleNode(xpath);
node.Attributes["value"].Value = keyValue;
document.Save(filePath);
*/
XmlDocument document = new XmlDocument();
document.Load(filePath);
XmlNodeList nodes = document.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute attribute = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (attribute != null && (attribute.Value == keyName))
{
attribute = nodes[i].Attributes["value"];
//对目标元素中的第二个属性赋值
if (attribute != null)
{
attribute.Value = keyValue;
break;
}
}
}
document.Save(filePath);
}
/**//// <summary>
/// 读取程序的config文件的键值。
/// 如果键名不存在,返回空
/// </summary>
/// <param name="keyName">键名</param>
/// <returns></returns>
public string AppConfigGet(string keyName)
{
string strReturn = string.Empty;
try
{
XmlDocument document = new XmlDocument();
document.Load(filePath);
XmlNodeList nodes = document.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性
XmlAttribute attribute = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素
if (attribute != null && (attribute.Value == keyName))
{
attribute = nodes[i].Attributes["value"];
if (attribute != null)
{
strReturn = attribute.Value;
break;
}
}
}
}
catch
{
;
}
return strReturn;
}
/**//// <summary>
/// 获取指定键名中的子项的值
/// </summary>
/// <param name="keyName">键名</param>
/// <param name="subKeyName">以分号(;)为分隔符的子项名称</param>
/// <returns>对应子项名称的值(即是=号后面的值)</returns>
public string GetSubValue(string keyName, string subKeyName)
{
string connectionString = AppConfigGet(keyName).ToLower();
string[] item = connectionString.Split(new char[] {‘;‘});
for (int i = 0; i < item.Length; i++)
{
string itemValue = item[i].ToLower();
if (itemValue.IndexOf(subKeyName.ToLower()) >= 0) //如果含有指定的关键字
{
int startIndex = item[i].IndexOf("="); //等号开始的位置
return item[i].Substring(startIndex + 1); //获取等号后面的值即为Value
}
}
return string.Empty;
}
}
AppConfig测试代码:
public class TestAppConfig
{
public static string Execute()
{
string result = string.Empty;
//读取Web.Config的
AppConfig config = new AppConfig();
result += "读取Web.Config中的配置信息:" + "/r/n";
result += config.AppName + "/r/n";
result += config.AppConfigGet("WebConfig") + "/r/n";
config.AppConfigSet("WebConfig", DateTime.Now.ToString("hh:mm:ss"));
result += config.AppConfigGet("WebConfig") + "/r/n/r/n";
//读取*.App.Config的
config = new AppConfig("TestUtilities.exe.config");
result += "读取TestUtilities.exe.config中的配置信息:" + "/r/n";
result += config.AppName + "/r/n";
result += config.AppConfigGet("AppConfig") + "/r/n";
config.AppConfigSet("AppConfig", DateTime.Now.ToString("hh:mm:ss"));
result += config.AppConfigGet("AppConfig") + "/r/n/r/n";
return result;
}
}
用于获取或设置Web.config/*.exe.config中节点数据的辅助类的更多相关文章
- app.config *.exe.config 和*.vshost.exe.config基础学习
一.问题描述 在使用config文件来保存一些参数,便于下次启动程序时自动加载上次设置的参数的功能时, 碰到个问题,vs2010下调试运行程序始终无法实现config记录上次参数值,而直接运行exe程 ...
- JS window对象 Location对象 location用于获取或设置窗体的URL,并且可以用于解析URL。 语法: location.[属性|方法]
Location对象 location用于获取或设置窗体的URL,并且可以用于解析URL. 语法: location.[属性|方法] location对象属性图示: location 对象属性: lo ...
- 学习笔记_Java get和post区别(转载_GET一般用于获取/查询资源信息,而POST一般用于更新资源信息)
转载自:[hyddd(http://www.cnblogs.com/hyddd/)] 总结一下, Get是向服务器发索取数据的一种请求 而Post是向服务器提交数据的一种请求,在F ...
- 【VC++技术杂谈002】打印技术之获取及设置系统默认打印机
本文主要介绍如何获取以及设置系统的默认打印机. 1.获取系统中的所有打印机 获取系统中的所有打印机可以使用EnumPrinters()函数,该函数可以枚举全部的本地.网络打印机信息.其函数原型为: B ...
- OpenCV获取与设置像素点的值的几个方法
Title: OpenCV OpenCV像素值的获取与设置 Fn 1 : 使用 Mat 中对矩阵元素的地址定位的知识 (参考博文:OpenCV中对Mat里面depth,dims,channels,st ...
- C# asp.net IIS 在web.config和IIS中设置Session过期时间
有时候在web.config设置sessionState 或者类文件里设置Session.Timeout,在IIS里访问时每次都是达不到时间就超时,原因是因为在IIS中设置了Session的超时时间, ...
- web.config详解(配置文件节点说明)
转载:http://www.zzzj.com/html/20081110/67614.html web.config文件是一个XML文件,它的根结点是<configuration>,在&l ...
- App.config和Web.config配置文件的配置节点的解析
前言 在http://www.cnblogs.com/aehyok/p/3558661.html这篇博文中,大致对配置文件有了初步的了解,并且在文中有提到过<appSettings>和&l ...
- 程序集、应用程序配置及App.config和YourSoft.exe.config .
转自:http://www.cnblogs.com/luminji/archive/2010/10/21/1857339.html 什么是程序集 程序集标识属性 强名称的程序集 强名称工作原理 配置文 ...
随机推荐
- MongoDB水平分片集群(转)
为何需要水平分片 1 减少单机请求数,将单机负载,提高总负载 2 减少单机的存储空间,提高总存空间. 下图一目了然: mongodb sharding 服务器架构 简单注解: 1 mongos 路由进 ...
- ViewPager总结
https://github.com/youth5201314/banner compile 'com.youth.banner:banner:1.4.9' private void setBanne ...
- windowsphone8.1学习笔记之应用数据(二)
上一篇说了应用数据的应用设置,这篇说说应用文件,应用文件主要分为三种:本地应用文件.漫游应用文件和临时应用文件. 获取根目录方法如下,都是返回一个StorageFolder对象(稍后介绍这个). // ...
- Python爬虫--Urllib库
Urllib库 Urllib是python内置的HTTP请求库,包括以下模块:urllib.request (请求模块).urllib.error( 异常处理模块).urllib.parse (url ...
- Django项目高频使用文件
数据库配置: MySQL数据库 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'HOST': 'localhost' ...
- 小记tensorflow-1:tf.nn.conv2d 函数介绍
tf.nn.conv2d函数介绍 Input: 输入的input必须为一个4d tensor,而且每个input的格式必须为float32 或者float64. Input=[batchsize,im ...
- 原生Base64编码/解码(OC与Swift)
Objective-C NSString *plainString = @"foo"; Encoding NSData *plainData = [plainString data ...
- 【Leetcode-easy】Remove Nth Node From End of List
思路1:设置两个指针p1,p2指向表头,p1先走n步.再两个指针同时走.当p1指针指到链表尾部时,P2指针已经在需要删除节点的前一位.一定要注意一些细节. class ListNode { int v ...
- STL容器元素应满足的条件
要使用C++中的标准模板库中的容器,其元素要满足以下三个条件: 元素必须可以通过copy构造函数进行复制,且二者进行相等测试返回true. 元素必须可以通过赋值操作符完成赋值操作. 元素必须可以通过析 ...
- redhat5.1上安装oracle 10.2g客户端及配置使用
一)安装 1.命令 rpm -q gcc make binutils setarch compat-db compat-gcc compat-gcc-c++ compat-libstdc++ comp ...