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;

}

}

最新文章

  1. HTML5播放暂停音乐
  2. 如何在IamgeButton上面添加文字
  3. poj2642 The Brick Stops Here(DP基础题)
  4. 【转】分布式理论-CAP理论
  5. (五)、nodejs使用bootstrap的样式进行分页
  6. filter在CSS中的效果
  7. Linux内核监控模块-2-系统调用表地址的获取(Linux内核版本3.13)
  8. MySQL如何有效地创建基于 INNODB 引擎的表
  9. codevs 1282 约瑟夫问题(线段树)
  10. 002-C语言概览
  11. Struts2源代码解读之Action调用
  12. PyCharm 使用简介
  13. python 基础学习1
  14. TM3、4波段GeoTiff数据计算NDVI
  15. 前端架构之路:使用Vue.js开始第一个项目
  16. mysql中int(10)与int(11)有什么区别吗?
  17. python之路第四篇(基础篇)
  18. cinder块存储 后端采用lvm、nfs安装配置
  19. Dedecms列表页标签list/pagelist使用方法及pagelist的样式
  20. POST方式提交乱码解决

热门文章

  1. 献给写作者的 Markdown 新手指南及语法
  2. PowerBuilder -- 数字金额大写
  3. 多媒体开发之sps---解析sps得到图像的宽高
  4. Android 红色小圆球提示气泡 BadgeView
  5. python 基础 1.4 python运算符
  6. 九度OJ 1024:畅通工程 (最小生成树)
  7. vue+vuex构建单页应用
  8. python 函数中的递归、lambda 、map reduce 等详解
  9. Java7、Java8 安装卸载问题
  10. Java多线程系列 基础篇07 synchronized底层优化