@rhodanny , you can try to use FormClosing event to write data to file and read the last line of the file if you want to access the last DateTime.Now.
Here is a code example you could refer to.
private void Form1_Load(object sender, EventArgs e)
{
string applicationPath = Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory); // the directory that your program is installed in
string saveFilePath = Path.Combine(applicationPath, "1.txt");
if(File.Exists(saveFilePath))
{
string text = File.ReadAllLines(saveFilePath).LastOrDefault();
label1.Text=text;
}
else
{
label1.Text="the first time you come in ";
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
string applicationPath = Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory); // the directory that your program is installed in
string saveFilePath = Path.Combine(applicationPath, "1.txt");
StreamWriter w = new StreamWriter(saveFilePath, true);
w.WriteLine(DateTime.Now.ToString());
w.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string str = DateTime.Now.ToString();
MessageBox.Show(str);
}
Result:
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.