Preserve data between application executions

后端 未结 4 1403
长发绾君心
长发绾君心 2020-12-01 17:03

I have an application with some textboxes. My user fills the textboxes and runs some methods, when they close the application data is lost (normally).

I want to ke

4条回答
  •  囚心锁ツ
    2020-12-01 17:21

    Simplest way is binding your textboxes to application settings:

    • select texbox you want to preserve
    • go to Properties > Data > (ApplicationSettings)
    • add application settings binding to Text property
    • on FormClosed event save application settings

    Saving settings:

    private void Form_FormClosed(object sender, FormClosedEventArgs e)
    {
        Settings.Default.Save();
    }
    

    Next time when user will start your application, settings will be loaded from user-specific file, and textboxes will be filled with same data as it was before user closed an application last time.

    Also in application settings you can store local variables, but you will have to add settings for them manually, and manually read that setting on application start:

    • open Properties folder under project > Settings.settings
    • add settings you want to store (e.g. MyCounter)
    • set MyCounter type, scope, and default value (e.g. int, User, 0)
    • read setting to your local variable var x = Settings.Default.MyCounter
    • on form closed save setting Settings.Default.MyCounter = x just before calling Settings.Default.Save()

提交回复
热议问题