修改配置文件app.config时出现值叠加的问题,求解???
一个很简单的需求:使用程序动态修改配置文件。
实现方法:
///<summary> ///向.config文件的appKey结写入信息AppValue 保存设置 ///</summary> ///<param name="AppKey">节点名</param> ///<param name="AppValue">值</param> private void SetValue(string AppKey, string AppValue) { //Update XmlDocument xDoc = new XmlDocument(); xDoc.Load(_crmUrl); XmlNode xNode; xNode = xDoc.SelectSingleNode("//appSettings"); XmlElement xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']"); XmlElement xElem2; 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(_crmUrl); }
<add key="cs" value="Data Source=192.0.1.122;Initial Catalog=DB;Persist Security Info=True;User ID=sa;Password=sa" />
<add key="cs" value="Data Source=192.0.1.122;Initial Catalog=DB;Persist Security Info=True;User ID=sa;Password=saData Source=192.0.1.122;Initial Catalog=DB;Persist Security Info=True;User ID=sa;Password=sa" />