How can I save a variable even after the program is exited?
Hi, basically all I want to do is code my project so that it stores any saved variables for the next time the program is run. At the moment once I close and reopen the program it obviously resets any variables. Is there any simple way to do this (please bear in mind, im very new at programming)
Il give you an example. If I have a simple program that when I press a button it adds 1 to a counter. Now next time I open that program and press the button again I want to add to the previous counter.
Thanks in advance
Re: How can I save a variable even after the program is exited?
You will have to either create a file, save information to a database or save information to the registry. That being said, please don't use the registry unless you really need to. If it is just basic info for your application, create a file and save the variables in there. Search the forum and you will find numerous examples.
Re: How can I save a variable even after the program is exited?
The most simple approach would be to save your variable values in a file. When your program starts it would open the file and your variables will be initialized with the file's contents. There are other methods:database and the registry but I believe the file method is your ticket.
Re: How can I save a variable even after the program is exited?
I didn't realize that sotoasty was writing when I was. So that explains the double similar response :)
Re: How can I save a variable even after the program is exited?
Quote:
Originally Posted by
viperbyte
The most simple approach would be to save your variable values in a file. When your program starts it would open the file and your variables will be initialized with the file's contents. There are other methods:database and the registry but I believe the file method is your ticket.
Thanks for your replies. This sounds the most simple way.
Could tell me how to code my program so it opens the file with the variables and saves the file with the variables upon exit? Or direct me to somewhere that explains it?
That would be greatly appreciated :)
Thanks
Re: How can I save a variable even after the program is exited?
I don't know what your intentions are for future programming. If you're going to be a programmer or just take classes you would benifit most by trying to code this. You can search you Visual Studio Help if you have VS or search the internet for the basic snippets. The snippets would be how to open a file, how to read a file, how to save to a file(in C#). The examples might hard code the values that will be written. you replace that with your variables. But if you are not going to be a programmer or take classes on programming and this is just a one time, then I can understand why you won't want to be bothered with all the coding stuff. I'd share some code with you if i had it. But i don't. Sorry.
Re: How can I save a variable even after the program is exited?
Look at the streamreader and streamwriter classes contained in the system.io
Re: How can I save a variable even after the program is exited?
For stuff where you don't know where to start, google ftw.
I also recommend getting a good beginner's book. EVERY c sharp book will tell you how variables work, and how to save information to a file.