Marcus, as bigown
pointed out, it is not possible to keep a variable in memory, however you can save a flag
stating that the system needs to be restarted, so the easiest solution is to change 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 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, storing this data using Sqlite with SQLite Encryption Extension (SEE). you can see more about it in.:
Finally you can send a request to a WebAPI
and it will update a flag
stating 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 WebAPI
.
As to whether the machine has been restarted, you can read the Windows logs in the following way, then you just have to look at 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);
}