Marcus, as Maniero pointed out, you can’t keep a variable in memory, but you can save a flag stating that the system needs to be restarted, perhaps the easiest solution is to change the app.config
.
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["Restart"].Value = "true";
config.AppSettings.Settings["Data"].Value = DateTime.Now.ToString("o");
config.Save(ConfigurationSaveMode.Modified);
if you need more data, you can choose to store a JSON file in some system folder (the example below uses the Newtonsoft.Json
):
var dados = new { MustRestart = true, Data = DateTime.Now, Foo = "Hello", Bar = "World" };
var json = JsonConvert.SerializeObject(dados);
System.IO.File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "\dados.json", json);
If you have some kind of paranoia and want to prevent the user from modifying these values, you have two options, to store this data using Sqlite with SEE(Sqlite Encryption Extension). you can see more about.:
finally you can send a request to a WebAPI
and this will update a flag
informing that the machine needs to be restarted, this approach has two negative points, the client needs an internet connection and you need to implement and host the WebAPI
.
As to whether the machine has been restarted, you can read the Windows records as follows, then just scan the entries.:
var eventLog = new System.Diagnostics.EventLog();
//eventLog.Source = ""; O ideal que informe o nome do Log que possui os eventos de inicialização do sistema.
foreach (var entry in eventLog.Entries)
{
Console.WriteLine(entry.Message);
}
I’ll take a look, thanks. I am currently using this feature: http://stackoverflow.com/questions/1474293/how-to-programtically-find-the-last-login-time-to-a-machine it saves the date in a file and if this date has remained unchanged it still requires reboot, but it would give a false positive if it simply left the user, but I do not have so much "paranoia" as to kkkkk, Thanks and friend.
– Marcus André