m.edmondson
m.edmondson

Reputation: 30922

What the accepted way to persist variables in a windows service

I have a windows service which will be running all the time, but will only activate if it has been at least 24 hours since the last time. Therefore I need to store the time and date since it last carried out it's payload.

To do this I was thinking of writing to the App.Config with:

ConfigurationManager.AppSettings.Set("LastRunTime", DateTime.Now.ToString());

however this doesn't seem to persist, but rather cache which is no good if the service is restarted.

So what is the accepted method for persisting values for a windows service?

Upvotes: 3

Views: 2092

Answers (2)

m.edmondson
m.edmondson

Reputation: 30922

I ended up serializing the object as per this article and inline with what @ChrisBint said I saved it into a text file. This is what I was looking for as 'accepted', I knew I could write to text files but it was the method I was looking for. I wanted to keep everything structured and strongly typed instead of writing my own methods for persisting data.

Since @Chris only provided a part answer I've upvoted him and accepted this.

Upvotes: 0

Kinexus
Kinexus

Reputation: 12904

Use a database, simple xml/text file, registry value.

Also, based on your requirements, a simple console app run using Task scheduler may be another option.

Upvotes: 7

Related Questions