1

I have a Console Application that needs to store LastRunDate (time when it was ran last time). It is common date for all users on a machine. Each time a user run this app this value should be updated.

Where to store that value? Using ConfigurationManager.AppSettings? This is read only. Using settings file with serialized settings - Application scope? - This is also read-only User scope? - this will be different value for each user.

Dale K
  • 25,246
  • 15
  • 42
  • 71
jlp
  • 9,800
  • 16
  • 53
  • 74
  • Here is how you can update values in AppSettings: [link](http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/d68a872e-14bc-414a-82c4-d1035a11b4a8/) Update: Try [this](http://stackoverflow.com/questions/4934898/reload-configuration-settings-from-an-external-config-file-during-run-time) to work with different settings files. – Eugene Mar 26 '12 at 11:02
  • OK, but it does not work if one decide to store AppSettings section in separate file. – jlp Mar 26 '12 at 11:30
  • @jlp have you read my answer? – Massimiliano Peluso Mar 26 '12 at 12:13
  • Updated with a link to answer that shows how you can work with different files. – Eugene Mar 26 '12 at 12:15
  • What Massimilano Peluso is trying to show you is a cleaner solution. The walkthrough on the concept is here: http://msdn.microsoft.com/en-us/library/et91as27.aspx – Eugene Mar 26 '12 at 12:30
  • `ConfigurationManager.AppSettings` are not read only - you can write to them. I'm not sure its the correct place though. I would write a text file in the application directory. – Dale K May 31 '20 at 21:00

2 Answers2

2

you could create a class that encapsulate all the properties you need - DateTime,user,etc that you can user the Serialization to "Save" the state of that object an Deserialize it when you need to update the state on the object (in your case the Last run Date)

more info at :

http://msdn.microsoft.com/en-us/library/ms233843.aspx

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • +1, that is a clean way to do this. Looks a bit harder than just messing with config files, but it is much cleaner. – Eugene Mar 26 '12 at 12:30
0

Just store it in your settings, user scoped, such as:

Properties.Settings.Default.LastRunDate = DateTime.Now;
Properties.Settings.Default.Save();

You can expand your 'Properties' node in Visual Studio to access the Settings and define this 'LastRunDate' setting.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • It is only possible with User scope. But then each user will have its own date. It have to be one date. – jlp Mar 26 '12 at 11:03
  • Hm, understandable; though for something so simple you could simply write it to a dedicate file - if you even _need_ to do that: perhaps accessing the [last access date of the executable](http://msdn.microsoft.com/en-us/library/system.io.file.getlastaccesstime(classic).aspx) would suit your needs, removing the requirement to store anything anywhere. This all depends on your situation. – Grant Thomas Mar 26 '12 at 11:10