Hello,
First inside of the form's load event add this code:
if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "ApplicationState") == false)
{
System.IO.StreamWriter objFile = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "ApplicationState");
objFile.Write("ClosedSucessfully");
objFile.Close();
LoadFile();
}
else
{
System.IO.StreamReader objFile = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "ApplicationState");
if (objFile.ReadToEnd() == "")
{
LoadBackup();
}
else
{
LoadFile();
}
objFile.Close();
System.IO.StreamWriter WriteToFile = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "ApplicationState");
WriteToFile.Close();
}
Timer Timer = new Timer { Interval = 30000, Enabled = true };
Timer.Tick += (s, eventArfs) =>
{
SaveBackup();
};
Now inside of the formclosing event add these statments:
System.IO.StreamWriter objFile = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "ApplicationState");
objFile.Write("ClosedSuccessfully");
objFile.Close();
Finally add these procedures to your code:
private void SaveBackup()
{
System.IO.StreamWriter objFile = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "Backup.txt");
objFile.Write(txtEditor.Text);
objFile.Close();
}
private void LoadBackup()
{
if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + "Backup.txt"))
{
System.IO.StreamReader objFile = new System.IO.StreamReader(AppDomain.CurrentDomain.BaseDirectory + "Backup.txt");
txtEditor.Text = objFile.ReadToEnd();
objFile.Close();
}
}
private void LoadFile()
{
//Add your code here to load the normal file...
}
This code assumes that the textbox is named: txtEditor. I hope it helps.