| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System;
- using System.Configuration;
- using System.IO;
- using System.Web;
- using System.Xml;
- namespace SQ12345_OutApi.Utility
- {
- public class ConfigHelper
- {
- static XmlDocument xDoc;
- static XmlNode xNode;
- static ConfigHelper()
- {
- lasttime = new FileInfo(path).LastWriteTime;
- xDoc = new XmlDocument();
- xDoc.Load(path);
- xNode = xDoc.SelectSingleNode("//appSettings");
- }
- static string path = HttpContext.Current.Server.MapPath("~/Configs/system.config");
- static DateTime lasttime = DateTime.Now;
- /// <summary>
- /// 根据Key取Value值
- /// </summary>
- /// <param name="key"></param>
- public static string GetValue(string key)
- {
- try
- {
- DateTime dt = new FileInfo(path).LastWriteTime;
- if (dt != lasttime)
- {
- xDoc = new XmlDocument();
- xDoc.Load(path);
- xNode = xDoc.SelectSingleNode("//appSettings");
- lasttime = dt;
- }
- var xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
- return xElem1?.GetAttribute("value") ?? "";
- }
- catch
- {
- return "";
- }
- }
- /// <summary>
- /// 根据Key修改Value
- /// </summary>
- /// <param name="key">要修改的Key</param>
- /// <param name="value">要修改为的值</param>
- public static void SetValue(string key, string value)
- {
- try
- {
- DateTime dt = new FileInfo(path).LastWriteTime;
- if (dt != lasttime)
- {
- xDoc = new XmlDocument();
- xDoc.Load(path);
- xNode = xDoc.SelectSingleNode("//appSettings");
- lasttime = dt;
- }
- XmlElement xElem1;
- XmlElement xElem2;
- xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
- if (xElem1 != null) xElem1.SetAttribute("value", value);
- else
- {
- xElem2 = xDoc.CreateElement("add");
- xElem2.SetAttribute("key", key);
- xElem2.SetAttribute("value", value);
- xNode.AppendChild(xElem2);
- }
- xDoc.Save(path);
- }
- catch
- { }
- }
- }
- }
|