3

How can I declare a variable which would save sting forever?

I mean if the user closes and restarts the program, this string value is not lost.

How can this be done?

Eriawan Kusumawardhono
  • 4,796
  • 4
  • 46
  • 49
Irakli Lekishvili
  • 33,492
  • 33
  • 111
  • 169

4 Answers4

7

There are a number of different ways to store state for an application. The approach really depends on the type of data you are storing and other requirements

Options

ccozad
  • 1,119
  • 8
  • 13
  • 1
    Don't forget config files! Though I would only tend to use them for deployed state. – Reddog Sep 22 '11 at 22:43
  • thanks i know that. I forgot to say which is safest way so in cofig file anyone can see content – Irakli Lekishvili Sep 22 '11 at 22:45
  • So you have information that is sensative or you don't otherwise want seen? All of the options could be stored in an encrypted form. The remote web service or cloud option also gives you the chance to revoke access if needed. – ccozad Sep 22 '11 at 22:49
3

Save the variable value to a database or other storage.

Orn Kristjansson
  • 3,435
  • 4
  • 26
  • 40
  • Database is most popular and easiest to move to another program if you want. Other alternatives are config/property files or registry settings. – corsiKa Sep 22 '11 at 22:36
  • It's tagged `WPF`, so it's a Windows app, not a web app. – Jordan Sep 22 '11 at 22:36
2

You could save it to a file, a database, a USB drive, somewhere in the cloud... somewhere other than the computer's memory.

Here's a quick example in C# (to write to a file):

string someString = "I will be here forever... well kind of";

using (StreamWriter outfile = new StreamWriter(@"C:\myfile.txt"))
{
   outfile.Write(someString);
}
Bryan Crosby
  • 6,486
  • 3
  • 36
  • 55
1

I recommend a database, in particular SQLite.

Jordan
  • 31,971
  • 6
  • 56
  • 67