纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.4 KiB

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