C# 修改APPconfig设置

来源:互联网 发布:知党章党规,系列讲话 编辑:程序博客网 时间:2024/06/05 02:34
#region  修改APPconfig设置          public static void SetConfigValue(string AppKey, string AppValue)        {            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();            xDoc.Load(Application.StartupPath + "//LedInfoSystem.dll.config");            System.Xml.XmlNode xNode;            System.Xml.XmlElement xElem1;            System.Xml.XmlElement xElem2;            xNode = xDoc.SelectSingleNode("//appSettings");            xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");            if (xElem1 != null) xElem1.SetAttribute("value", AppValue);            else            {                xElem2 = xDoc.CreateElement("add");                xElem2.SetAttribute("key", AppKey);                xElem2.SetAttribute("value", AppValue);                xNode.AppendChild(xElem2);            }            xDoc.Save(Application.StartupPath + "//LedInfoSystem.dll.config");        }        public static string GetConfigValue(string AppKey)        {            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();            xDoc.Load(Application.StartupPath + "//LedInfoSystem.dll.config");            System.Xml.XmlNode xNode;            System.Xml.XmlElement xElem1;            xNode = xDoc.SelectSingleNode("//appSettings");            xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");            string appValue = xElem1.Value;            return appValue;        }        public static void SetWebServiceAddressValue(string AppValue)        {            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();            xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");            System.Xml.XmlNode xNode;            System.Xml.XmlElement xElem1;            xNode = xDoc.SelectSingleNode("//client");            xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//endpoint[@address]");            if (xElem1 != null) xElem1.SetAttribute("address", AppValue);            xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");        }        public static string GetWebServiceAddressValue()        {            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();            xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");            System.Xml.XmlNode xNode;            System.Xml.XmlElement xElem1;            xNode = xDoc.SelectSingleNode("//client");            xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//endpoint[@address]");            string strAddress = xElem1.GetAttribute("address").ToString();            return strAddress;        }        #endregion
0 0