Brak opisu

Configs.cs 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Configuration;
  2. using System.Web;
  3. namespace CallCenter.Utility
  4. {
  5. public 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. try
  22. {
  23. System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
  24. xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
  25. System.Xml.XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
  26. var xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
  27. return xElem1?.GetAttribute("value") ?? "";
  28. }
  29. catch
  30. {
  31. return "";
  32. }
  33. }
  34. /// <summary>
  35. /// 根据Key修改Value
  36. /// </summary>
  37. /// <param name="key">要修改的Key</param>
  38. /// <param name="value">要修改为的值</param>
  39. public static void SetValue(string key, string value)
  40. {
  41. try
  42. {
  43. System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
  44. xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
  45. System.Xml.XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
  46. //System.Xml.XmlNode xNode;
  47. System.Xml.XmlElement xElem1;
  48. System.Xml.XmlElement xElem2;
  49. xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
  50. if (xElem1 != null) xElem1.SetAttribute("value", value);
  51. else
  52. {
  53. xElem2 = xDoc.CreateElement("add");
  54. xElem2.SetAttribute("key", key);
  55. xElem2.SetAttribute("value", value);
  56. xNode.AppendChild(xElem2);
  57. }
  58. xDoc.Save(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
  59. }
  60. catch
  61. { }
  62. }
  63. }
  64. }