县级监管平台

Configs.cs 2.2KB

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