Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve data between application executions

Tags:

c#

.net

winforms

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 keep the value of a couple of textboxes and some local variables. It's not worth it to use database, and simple .txt files are not clean enough, is there any other simple and brief way of storing little volumes of data between application runs?

I'm not sure but have heard some wisps about resource files, are they good for this case?

like image 771
Mahdi Tahsildari Avatar asked Dec 24 '12 10:12

Mahdi Tahsildari


3 Answers

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()
like image 50
Sergey Berezovskiy Avatar answered Nov 11 '22 15:11

Sergey Berezovskiy


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/

like image 44
mcalex Avatar answered Nov 11 '22 15:11

mcalex


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.

like image 1
TalentTuner Avatar answered Nov 11 '22 14:11

TalentTuner