Reputation: 344
I created a method that append data to file .txt and it executes on Button_Click method. But I want to save the data to file not when I click the button but after time automatically (ex. after couple minutes starting from Application_Start). How can I solve my problem?
static public bool appendToFileTxt(string input)
{
try
{
if (File.Exists(nameOfFile))
File.Copy(nameOfFile, nameOfFile + ".bak", true);
using (StreamWriter sw = new StreamWriter(nameOfFile, true)){
sw.WriteLine(input.Replace("\r", ""));
}
return true;
}
catch{
return false;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
appendToFileTxt(newInput);
}
Upvotes: 0
Views: 42
Reputation: 12196
You can use the Timer class for this usage.
// Simulate Application_Start
public static void Main()
{
var appendToFileTimer = new Timer(AppendToFile, null, TimeSpan.Zero, TimeSpan.FromSeconds(5));
}
public static void AppendToFile(Object state)
{
Console.WriteLine("Append to file");
}
Upvotes: 1