3

I have a WPF application, when the app runs I need to store a variable in a shared memory. When the app is being closed and successively restart I need to get the variable previously stored. I need a simple solution, I would avoid using a text file saved in some place. If the machine is restarted I do now need to get that variable.

Any idea what are my option?

eran otzap
  • 12,293
  • 20
  • 84
  • 139
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • 1
    If you don't mind me asking, is there any reason why you can't save to a text file? It would most likely be a lot more easier/simple. – Sam Jan 07 '14 at 14:52
  • 3
    Why can't you save it in appsettings ? – CodeWeed Jan 07 '14 at 14:55
  • 1
    @CodeWeed: isn't appsettings basically a text file? (Not that I know why that's a bad idea, but still...) GibboK: if you want to persist data after your app is shut down then you have to to save it 8somewhere* - that's going to be either a file or perhaps the registry. Why don't you want to use a file? – Dan Puzey Jan 07 '14 at 15:20

2 Answers2

2

You could use your Settings file to save properties between application sessions.

For more information see this and note the Saving User Settings at Run Time section. It technically is "saved" to a file, but in a much dev-friendly manner.

Edit: That is a link for VS2005, oops! Use ConfigurationManager.AppSettings and refer to this post when saving for newer apps.

Community
  • 1
  • 1
Kcvin
  • 5,073
  • 2
  • 32
  • 54
  • 1
    @eran I know the title suggests that ... but the detail is about persisting across machine restarts. – richaux Jan 07 '14 at 16:17
  • yes i get that , it just seemed obvious that he meant that as an alternative to writing to any files , where he said text file he probably meant any file – eran otzap Jan 07 '14 at 16:27
0

If you're wanting to avoid writing/reading files, you could use the Registry.

poy
  • 10,063
  • 9
  • 49
  • 74