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中节点数据的辅助类的更多相关文章

  1. app.config *.exe.config 和*.vshost.exe.config基础学习

    一.问题描述 在使用config文件来保存一些参数,便于下次启动程序时自动加载上次设置的参数的功能时, 碰到个问题,vs2010下调试运行程序始终无法实现config记录上次参数值,而直接运行exe程 ...

  2. JS window对象 Location对象 location用于获取或设置窗体的URL,并且可以用于解析URL。 语法: location.[属性|方法]

    Location对象 location用于获取或设置窗体的URL,并且可以用于解析URL. 语法: location.[属性|方法] location对象属性图示: location 对象属性: lo ...

  3. 学习笔记_Java get和post区别(转载_GET一般用于获取/查询资源信息,而POST一般用于更新资源信息)

    转载自:[hyddd(http://www.cnblogs.com/hyddd/)] 总结一下,      Get是向服务器发索取数据的一种请求      而Post是向服务器提交数据的一种请求,在F ...

  4. 【VC++技术杂谈002】打印技术之获取及设置系统默认打印机

    本文主要介绍如何获取以及设置系统的默认打印机. 1.获取系统中的所有打印机 获取系统中的所有打印机可以使用EnumPrinters()函数,该函数可以枚举全部的本地.网络打印机信息.其函数原型为: B ...

  5. OpenCV获取与设置像素点的值的几个方法

    Title: OpenCV OpenCV像素值的获取与设置 Fn 1 : 使用 Mat 中对矩阵元素的地址定位的知识 (参考博文:OpenCV中对Mat里面depth,dims,channels,st ...

  6. C# asp.net IIS 在web.config和IIS中设置Session过期时间

    有时候在web.config设置sessionState 或者类文件里设置Session.Timeout,在IIS里访问时每次都是达不到时间就超时,原因是因为在IIS中设置了Session的超时时间, ...

  7. web.config详解(配置文件节点说明)

    转载:http://www.zzzj.com/html/20081110/67614.html web.config文件是一个XML文件,它的根结点是<configuration>,在&l ...

  8. App.config和Web.config配置文件的配置节点的解析

    前言 在http://www.cnblogs.com/aehyok/p/3558661.html这篇博文中,大致对配置文件有了初步的了解,并且在文中有提到过<appSettings>和&l ...

  9. 程序集、应用程序配置及App.config和YourSoft.exe.config .

    转自:http://www.cnblogs.com/luminji/archive/2010/10/21/1857339.html 什么是程序集 程序集标识属性 强名称的程序集 强名称工作原理 配置文 ...

随机推荐

  1. 机器学习三 -- 用Python实现K-近邻算法

    Python语言实现机器学习的K-近邻算法 写在前面 额...最近开始学习机器学习嘛,网上找到一本关于机器学习的书籍,名字叫做<机器学习实战>.很巧的是,这本书里的算法是用Python语言 ...

  2. Unity3D 与 objective-c 之间数据交互。iOS SDK接口封装Unity3D接口 .-- 转载

    Unity 3D 简单工程的创建.与Xcode 导出到iOS 平台请看这 Unity3D 学习 创建简单的按钮.相应事件 Unity C# 代码 using UnityEngine; using Sy ...

  3. T-SQL简单查询语句(模糊查询)

    T-SQL简单查询语句 简单查询: 1.最简单查询(查所有数据) select * from 表名: 注:* 代表所有列 select * from info 2.查询指定列 select code, ...

  4. 五个知识体系之-SQL学习-第一天

    1. 创建数据库 CREATE DATABASE test1; 2. 删除数据库 DROP DATABASE test1; 3. 创建表 CREATE TABLE tabname (userid BI ...

  5. HTML 学习笔记 JQuery(animation)

    动画效果也是JQuery库吸引人的地方,通过JQuery的动画方法,能够轻松的为网页天假非常紧菜的视觉效果. show()方法和hide()方法 show()方法和hide()方法是JQuery中最基 ...

  6. 聚聚科技——php开发笔试题及答案

    聚聚科技是一个刚创立的公司,很小很小,人很少,老板感觉是个典型的北京小伙儿,戾气很重,很有个性.笔试题倒是简单: 1. echo(), print(), print_r()的区别? echo是PHP语 ...

  7. kinect/xiton 的环境搭建 + rgb图像和深度图的标定

    ~ 软件下载地址 openni https://structure.io/openni https://github.com/OpenNI/OpenNI2 其他软件建议直接下载或通过某宝购买配套的 ~ ...

  8. DOM的构建与优化

    一.构建模块 HTML描述了一个页面的结构.浏览器会将它转换成一种他们能够理解的格式——文档对象模型(DOM),浏览器引擎有一段特殊的代码叫做解析器,将数据从一种格式转换成另外一种格式. 一个HTML ...

  9. php 获取上上个月数据 使用 strtotime('-1 months')的一个bug

    今天,使用php 日期函数处理数据,发现一个问题. 具体场景是这样的,我一直以为strtotime  格式化当前日期 或 指定日期可以找到对应的数据,比如我要查找上上个与的数据,因为我要获取当前时间的 ...

  10. nginx+keepalived简单双机主从热备

    双机主从热备概述 可以两台机子互为热备,平时各自负责各自的服务.在做上线更新的时候,关闭一台服务器的tomcat后,nginx自动把流量切换到另外一台服务的后备机子上,从而实现无痛更新,保持服务的持续 ...