人民医院API

ConfigHelper.cs 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Configuration;
  2. using System.Web;
  3. namespace RMYY_CallCenter_Api.Utility
  4. {
  5. public class ConfigHelper
  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. static string path = HttpContext.Current.Server.MapPath("~/Configs/system.config");
  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(path);
  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 void SetValue(string key, string value)
  41. {
  42. try
  43. {
  44. System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
  45. xDoc.Load(path);
  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(path);
  60. }
  61. catch
  62. { }
  63. }
  64. }
  65. }