1、添加System.Configuration.dll引用;程序中添加using System.Configuration;
读取数据:(tbHost为文本控件)
tbHost.Text = ConfigurationManager.AppSettings["host"].ToString();
2、修改、更新数据
private void btnOk_Click(object sender, EventArgs e){if (tbHost.Text.Trim().Equals("") || tbUserid.Text.Trim().Equals("")) return;// 修改并更新配置文件UpdateConfig("host", tbHost.Text.Trim());UpdateConfig("userid", tbUserid.Text.Trim());}private void UpdateConfig(string key,string value){// 通过Xml方式(需using System.xml;)//XmlDocument doc = new XmlDocument();//doc.Load(Application.ExecutablePath + ".config");//XmlNode node = doc.SelectSingleNode(@"//add[@key='" + key + "']"); // 定位到add节点//XmlElement element = (XmlElement)node;//element.SetAttribute("value", value); // 赋值//doc.Save(Application.ExecutablePath + ".config");//ConfigurationManager.RefreshSection("appSetting"); // 刷新节点// 利用ConfigurationConfiguration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);config.AppSettings.Settings[key].Value = value;config.Save(ConfigurationSaveMode.Full);ConfigurationManager.RefreshSection("appSettings");}