鄂尔多斯-招源科技

Configs.cs 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Configuration;
  2. using System.Web;
  3. namespace CallCenter.Utility
  4. {
  5. public static class Configs
  6. {
  7. public static System.Xml.XmlDocument xDoc;
  8. public static System.Xml.XmlNode xNode;
  9. static Configs()
  10. {
  11. xDoc = new System.Xml.XmlDocument();
  12. xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
  13. xNode = xDoc.SelectSingleNode("//appSettings");
  14. }
  15. /// <summary>
  16. /// 根据Key取Value值
  17. /// </summary>
  18. /// <param name="key"></param>
  19. public static string GetValue(string key)
  20. {
  21. var xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
  22. return xElem1?.GetAttribute("value") ?? "";
  23. }
  24. /// <summary>
  25. /// 根据Key修改Value
  26. /// </summary>
  27. /// <param name="key">要修改的Key</param>
  28. /// <param name="value">要修改为的值</param>
  29. public static void SetValue(string key, string value)
  30. {
  31. System.Xml.XmlElement xElem1;
  32. System.Xml.XmlElement xElem2;
  33. xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
  34. if (xElem1 != null) xElem1.SetAttribute("value", value);
  35. else
  36. {
  37. xElem2 = xDoc.CreateElement("add");
  38. xElem2.SetAttribute("key", key);
  39. xElem2.SetAttribute("value", value);
  40. xNode.AppendChild(xElem2);
  41. }
  42. xDoc.Save(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
  43. }
  44. }
  45. }