C#设置加载并保存

时间:2012-06-28 05:08:39

标签: c# winforms settings

我尝试使用此代码加载和保存设置但是当我关闭并重新运行程序时未加载的设置。默认情况下,设置为空白。我没有错误。

private void Form1_Load(object sender, EventArgs e)
{
      txtUsername.Text = Properties.Settings.Default.Username;
      txtPassword.Text = Properties.Settings.Default.Password;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
      Properties.Settings.Default.Username = txtUsername.Text;
      Properties.Settings.Default.Password = txtPassword.Text;
}

2 个答案:

答案 0 :(得分:6)

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  Properties.Settings.Default.Username = txtUsername.Text;
  Properties.Settings.Default.Password = txtPassword.Text;
  Properties.Settings.Default.Save();
}

答案 1 :(得分:3)

How To: Write User Settings at Run Time with C# 如果要在应用程序会话之间保留对设置的更改,请调用Save方法,如下所示:;

Properties.Settings.Default.Save();

您需要在Form1_FormClosing

的末尾调用Save()