思念食品API

Configs.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections.Generic;
  2. using System.Configuration;
  3. using System.Web;
  4. namespace CallCenter.Utility
  5. {
  6. public static class Configs
  7. {
  8. //public static System.Xml.XmlDocument xDoc;
  9. //public static System.Xml.XmlNode xNode;
  10. //static Configs()
  11. //{
  12. // xDoc = new System.Xml.XmlDocument();
  13. // xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
  14. // xNode = xDoc.SelectSingleNode("//appSettings");
  15. //}
  16. /// <summary>
  17. /// 根据Key取Value值
  18. /// </summary>
  19. /// <param name="key"></param>
  20. public static string GetValue(string key)
  21. {
  22. try
  23. {
  24. System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
  25. xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
  26. System.Xml.XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
  27. var xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
  28. return xElem1?.GetAttribute("value") ?? "";
  29. }
  30. catch
  31. {
  32. return "";
  33. }
  34. }
  35. /// <summary>
  36. /// 根据Key修改Value
  37. /// </summary>
  38. /// <param name="key">要修改的Key</param>
  39. /// <param name="value">要修改为的值</param>
  40. public static bool SetValue(string key, string value)
  41. {
  42. try
  43. {
  44. System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
  45. xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
  46. System.Xml.XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
  47. //System.Xml.XmlNode xNode;
  48. System.Xml.XmlElement xElem1;
  49. System.Xml.XmlElement xElem2;
  50. xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
  51. if (xElem1 != null) xElem1.SetAttribute("value", value);
  52. else
  53. {
  54. xElem2 = xDoc.CreateElement("add");
  55. xElem2.SetAttribute("key", key);
  56. xElem2.SetAttribute("value", value);
  57. xNode.AppendChild(xElem2);
  58. }
  59. xDoc.Save(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
  60. return true;
  61. }
  62. catch
  63. {
  64. return false;
  65. }
  66. }
  67. /// <summary>
  68. /// 获取配置列表
  69. /// </summary>
  70. /// <returns></returns>
  71. public static Dictionary<string, string> GetList()
  72. {
  73. Dictionary<string, string> paras = new Dictionary<string, string>();
  74. try
  75. {
  76. System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
  77. xDoc.Load(HttpContext.Current.Server.MapPath("~/Configs/system.config"));
  78. System.Xml.XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
  79. var enumer = xNode.SelectNodes("//add").GetEnumerator();
  80. while (enumer.MoveNext())
  81. {
  82. System.Xml.XmlElement xe = (System.Xml.XmlElement)enumer.Current;
  83. paras.Add(xe.GetAttribute("key"), xe.GetAttribute("value"));
  84. }
  85. }
  86. catch
  87. {
  88. }
  89. return paras;
  90. }
  91. }
  92. }