using System.Configuration; using System.Web; namespace NFine.Code { public class Configs { /// /// 根据Key取Value值 /// /// public static string GetValue(string key) { return ConfigurationManager.AppSettings[key].ToString().Trim(); } /// /// 根据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("~/Configs/system.config")); 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("~/Configs/system.config")); } } }