1、增加引用-->System.Configuration。
2、引用命名空间-->using System.Configuration。
3、代码:
//打开
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//修改配置文件
config.AppSettings.Settings["test"].Value = textBox1.Text;
//保存
config.Save();
//读取
textBox2.Text = System.Configuration.ConfigurationSettings.AppSettings["test"].ToString();
4、重启应用程序以应用配置:
if (MessageBox.Show("Restart the program to apply the new configuration!", "Reminder", MessageBoxButtons.OK) == DialogResult.OK)
{
Application.Restart();
}