winform在运行时读写app.config的操作的class。 using System; using System.Collections.Generic; using System.Text; using System.Xml; namespace T.Common { public class ConfigHelper { /// <summary> /// set appsettings key and value /// </summary> /// <param name="AppKey">AppKey</param> /// <param name="AppValue">AppValue</param> public static void SetValue(string AppKey, string AppValue) { XmlDocument xDoc = new XmlDocument(); //获取可执行文件的路径和名称 xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".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(System.Windows.Forms.Application.ExecutablePath + ".config"); } /// <summary> /// get app seting's value by key /// </summary> /// <param name="appKey">appKey</param> /// <returns>AppValue</returns> public static string GetConfigValue(string appKey) { XmlDocument xDoc = new XmlDocument(); try { xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config"); XmlNode xNode; XmlElement xElem; xNode = xDoc.SelectSingleNode("//appSettings"); xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']"); if (xElem != null) return xElem.GetAttribute("value"); else return ""; } catch (Exception) { return ""; } } } }