1

I'm developing a C# console application in .Net Framework 4.6.1, Visual Studio 2013, Windows 10 x64. I want to save last runtime timestamp in the Properties.Settings.Default.LastRuntime property settings every time when the application closes. It should work when user closes it by clicking on close [X] button, Ctrl+C, Ctrl+Break, windows shutdown, logoff events. Here is my code. Nothing seems to be working. LastRuntime property is not updating:

     static void Main(string[] args)
     {
        string lrt = Properties.Settings.Default.LastRuntime;
        Console.Write(lrt);
        Console.ReadLine();
        handler = new ConsoleEventDelegate(ConsoleEventCallback);
        SetConsoleCtrlHandler(handler, true);
    }

    // Handle application termination
    static bool ConsoleEventCallback(int eventType)
    {
        // Termination events: Ctrl+C = 0, Ctrl+Break = 1, Close/Cancel = 2, Logoff = 5, Shutdown = 6
        if (new[] { 0, 1, 2, 5, 6 }.Contains(eventType))
        {
            Properties.Settings.Default.LastRuntime = DateTime.UtcNow.ToString("yyyy'-'MM'-'dd'T'hh':'mm':'ss");
            Properties.Settings.Default.Save();
        }
        return false;
    }
    static ConsoleEventDelegate handler;
    private delegate bool ConsoleEventDelegate(int eventType);
    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool SetConsoleCtrlHandler(ConsoleEventDelegate callback, bool add);
Nima Derakhshanjan
  • 1,380
  • 9
  • 24
  • 37
Rohan W
  • 11
  • 2

2 Answers2

1

Multiple things going on here with a lot of SO posts relating to the matter, so I'll try to summarize for you:

  1. Application Settings scope - user vs application

In short, application scoped settings can't be changed at runtime while user scoped settings are designed be read/written at runtime.

Use user scoped settings

  1. Why are my application settings not getting persisted? (assuming you already use user settings)

If you have your Assembly info set to automatically generate any version numbers (1.0.*), then every time you debug your app the version number will be different, and so will be creating a new file every time.

If this is the case you will need to perform an upgrade on the settings file:

Properties.Settings.Default.Upgrade()

  1. C# Settings.Default.Save() not saving? To make sure the change is depicted in your code (if not restarting)

You need to call Default.Reload after calling the Save method:

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
0

If you want to capture it when the window closes, you can use the AppDomain.CurrentDomain.ProcessExit event

public static class Program
{
    public static void Main(string[] args)
    {
        AppDomain.CurrentDomain.ProcessExit += new EventHandler(ConsoleExit);           
    }

    private static void ConsoleExit(object sender, EventArgs e)
    {
        Properties.Settings.Default.LastRuntime = DateTime.UtcNow.ToString("yyyy'-'MM'-'dd'T'hh':'mm':'ss");
        Properties.Settings.Default.Save();
    }
}
Luke Parker
  • 299
  • 1
  • 10