Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintain a variable value between program runs

I have a simple c# console application that is scheduled after every 5 mins. Every invocation of the program requires the output of the last run.

What I am doing right now is using a text file and store the result in it. next time when it runs it opens the text file and know the output of the previous run.

Is there any other way to do it that wont require any such text file ? Like maintaining a session variable etc ?

like image 368
Akshay J Avatar asked Nov 21 '12 01:11

Akshay J


People also ask

What kind of information we will be storing in a variable?

Many variables store numbers and strings, like the ones above. Variables can also store other types of data, like lists, dictionaries, and Boolean values (true/false).

What is the purpose of a variable?

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information.

What must take place in a program before a variable is used?

What must take place in a program before a variable is used? The variable must be defined. What are the three primary activities of a program? Input, processing, and output.


2 Answers

You could use a settings file. Just add a Settings File to your project and save the value you want to store, each time the program closes. When the program starts have it check the value in the settings file.

like image 155
FrostyFire Avatar answered Sep 19 '22 06:09

FrostyFire


Well you have to save the data somewhere. Console apps don't have session variables (or even sessions). You probably mean environment variable. If you store it in an environment variable, the program will have to run from the same environment (i.e. from the same console window). If the machine crashes the environment vars are lost. You're probably better off with a file.

If this runs every 5 minutes, could you let the program sleep till it needs to run again? Then the data would be available in memory. It's still a problem if the machine crashes, so you still may need to keep a file as a backup.

If you don't want the users to see a file, and it's not too much data (though several K bytes would probably be workable), as @Quintin Robinson suggested, you could use the registry. Just make sure you're writing to an area that's logical and where you have the right permissions.

If you don't want the users to be able to see what's in the file you could scramble the contents so as to make it unreadable. If you use encryption, you'll need the key locally to decrypt so a skilled or determined attacker will still be able to get at the file. It may be better to just compress the data and call it good.

If you don't want the users to be able to easily change what's in the file you could store a checksum, hash, or HMAC with the data. You'll have to verify it locally, which means this can be attacked, but should stop a casual user from bit-hacking.

Of course you can combine registry storage, scrambling and checksum, depending on your needs and concerns.

I considered mentioning PStor and CryptProtectData/CryptUnprotectData but then I realized their security is USER based and so will not help here.

If the machine is connected to the Internet you could consider cloud storage, though I have no idea if that would be appropriate for your application.

like image 37
jimhark Avatar answered Sep 23 '22 06:09

jimhark