I have a console program that will be running in the background (to monitor changes in directories, then query database, etc, then sends email when there's a problem - based on the records that are saved/or not saved in the DB)..

I have counters, boolean values, etc that I maintain and use within the code as I progress from one line to another..

Now, I was asked - Can I make this program to "recover" if the program is "accidentally" terminated and opened again?

I was thinking of: when the console program is closed, I save all the variables, etc from a .txt file and when it starts up, it looks for that .txt file and get all the counters from there.

Is there any other way of doing this?