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");