Preserve data between application executions

There are a couple of options, but with most of them, you're going to be putting a file somewhere, whether it's a text file, resources/config or binary.

Using settings is one option: http://www.codeproject.com/Articles/17659/How-To-Use-the-Settings-Class-in-C

You can also take the serialization route: http://msdn.microsoft.com/en-us/library/vstudio/et91as27.aspx

Or you could possibly look into noSQL databases like MongoDB: http://www.mongodb.org/


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

You have the following options

  1. A local Microsoft Access database which can store small footprint.

  2. Use a Dictionary, Serialize / Deserialize to filesystem.

  3. The Windows registry.

Tags:

C#

.Net

Winforms