设置app.config中的某个key的value. 不指定

Else , 2009/05/06 16:23 , 代码片段,演示也有 , Comments(1) , Reads(2093) , Via Original
  /// <summary>
        /// 设置app.config中的某个key的value.
        /// </summary>
        /// <param name="AppKey">key</param>
        /// <param name="AppValue">value</param>
        public void SetValue(string AppKey, string AppValue)
        {
            XmlDocument xDoc = new XmlDocument();

            //此处配置文件在程序目录下
            xDoc.Load(Application.StartupPath + "\\PoCollect.exe.config");
            XmlNode xNode;
            XmlElement xElem1;
            XmlElement xElem2;
            xNode = xDoc.SelectSingleNode("//appSettings");
            xElem1 = (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 + "\\PoCollect.exe.config");
        }
帅哥
2009/11/18 08:58
确实好用啊。谢了
Pages: 1/1 First page 1 Final page
Add a comment

Nickname

Site URI

Email

Enable HTML Enable UBB Enable Emots Hidden Remember [Login] [Register]