ConfigHelper配置相关方法

2015-12-19 21:48:33  访问(1662) 赞(0) 踩(0)

//=====================================================================================
// All Rights Reserved ,
//=====================================================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;
using System.IO;
using System.Configuration;

namespace DotNet.Kernel
{
///
/// Config配置文件 公共帮助类
/// 版本:2.0
///

public class ConfigHelper
{
///
/// 根据Key取Value值
///

///
public static string GetValue(string key)
{
return GetValue(key, "");
}
///
/// 根据Key取Value值
///

///
///
public static string GetValue(string key, string defaultValue) {
String value = ConfigurationManager.AppSettings[key];
if (!string.IsNullOrEmpty(value))
{
return value.Trim();
}
return defaultValue;
}
///
/// 根据Key修改Value
///

/// 要修改的Key
/// 要修改为的值
public static void SetValue(string key, string value)
{
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.Load(HttpContext.Current.Server.MapPath("/XmlConfig/Config.xml"));
System.Xml.XmlNode xNode;
System.Xml.XmlElement xElem1;
System.Xml.XmlElement xElem2;
xNode = xDoc.SelectSingleNode("//appSettings");

xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
if (xElem1 != null) xElem1.SetAttribute("value", value);
else
{
xElem2 = xDoc.CreateElement("add");
xElem2.SetAttribute("key", key);
xElem2.SetAttribute("value", value);
xNode.AppendChild(xElem2);
}
xDoc.Save(HttpContext.Current.Server.MapPath("/XmlConfig/Config.xml"));
}

}
}

标签:ConfigHelper配置相关方法 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)