SharedPreferences but on c# Windows Form Apps C# Visual Studio

拟墨画扇 提交于 2021-02-11 14:19:38

问题


I'm creating a simple messages program, in which u can talk to other people.

Currently, I'm working on the login screen and I want to create a "remember me" option which will save your credentials the next time u enter the app. I worked a bit with android Studio,and Unity, and they both use something called SharedPreferences, I used it and it's great. So I wanna know if there is a SharedPreferences for C# and how to use it on a Windows Form App.

Any help is accepted.


回答1:


In winforms, we usually use Settings to keep the user setting info.

To achieve the requirement, you can refer to the following steps:

1.Click "Project" and choose "Setting Properties…", then choose "Settings"

2.Add new setting in "Settings"

3.Try the following code:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    // Save the current state of the checkbox
    Properties.Settings.Default.cb = checkBoxRememberMe.Checked;
    Properties.Settings.Default.str = textBox1.Text;
    Properties.Settings.Default.Save();
}

private void Form1_Load(object sender, EventArgs e)
{
    // load checkbox state from settings
    checkBoxRememberMe.Checked = Properties.Settings.Default.cb;
    textBox1.Text = Properties.Settings.Default.str;
}


来源:https://stackoverflow.com/questions/65142729/sharedpreferences-but-on-c-sharp-windows-form-apps-c-sharp-visual-studio

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