C# Setting load and save

巧了我就是萌 提交于 2019-12-24 08:28:45

问题


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;
}

回答1:


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();
}



回答2:


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



来源:https://stackoverflow.com/questions/11238398/c-sharp-setting-load-and-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!