C# Setting load and save

2019-07-25 08:42发布

I trying to load and save setting with this code but when I closing and rerun program the settings not loaded. By default the settings was blank. I have no error.

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条回答
够拽才男人
2楼-- · 2019-07-25 09:11

How To: Write User Settings at Run Time with C# If you want to persist the changes to the settings between application sessions, call the Save method, as shown below:;

Properties.Settings.Default.Save();

You need to call Save() at the end of Form1_FormClosing

查看更多
唯我独甜
3楼-- · 2019-07-25 09:12

try

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  Properties.Settings.Default.Username = txtUsername.Text;
  Properties.Settings.Default.Password = txtPassword.Text;
  Properties.Settings.Default.Save();
}
查看更多
登录 后发表回答