Unless this is to learn working with read/writing to and from a file, the better option is to create a settings file. In the example below I use NuGet package Newtonsoft.Json.
Settings file
{
"LastRanDateTime": "2021-12-21T03:59:00.0877276-08:00",
"TrackBarValue": 30
}
This class is the container for things you want to save and restore.
public class Setting
{
public DateTime? LastRanDateTime { get; set; }
public int TrackBarValue { get; set; }
}
Form code, save setting when closing the form and read them back in the Shown event.
using System;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace CodeSample
{
public partial class DemoForm : Form
{
private Setting _setting = new Setting();
// file will be in the application folder for settings
private readonly string _settingsFileName =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "settings.json");
public DemoForm()
{
InitializeComponent();
Shown += OnShown;
Closing += OnClosing;
}
private void OnShown(object sender, EventArgs e)
{
if (File.Exists(_settingsFileName))
{
_setting = JsonConvert.DeserializeObject<Setting>(File.ReadAllText(_settingsFileName));
if (_setting.LastRanDateTime.HasValue)
{
LastRanLabel.Text = $@"Last ran {_setting.LastRanDateTime.Value:h:mm:ss tt}";
}
trackBar1.Value = _setting.TrackBarValue;
}
else
{
LastRanLabel.Text = "?";
}
}
private void OnClosing(object sender, CancelEventArgs e)
{
_setting.LastRanDateTime = DateTime.Now;
_setting.TrackBarValue = trackBar1.Value;
File.WriteAllText(_settingsFileName, JsonConvert.SerializeObject(_setting, Formatting.Indented));
}
}
}