I'm coding a program for recording security logs in c#. But there are several problems.
First i create a .txt file to record logs. With:
FileStream fs1 = File.Create("C:\\" + filename + ".txt");
fs1.Close();
Then, in a loop i write something in the txt file with:
TextWriter tsw1 = new StreamWriter(@"C:\\" + filename + ".txt", true);
//Writing text to the file.
tsw1.WriteLine(curLogs + " \n");
//Close the file.
tsw1.Close();
After that, I set a timer in c# and every -for example- 10 mins i want to reset log file.
Timer timer = new Timer();
timer.Tick += new EventHandler(ik.timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (delay); // Timer will tick every 10 mins e.g
timer.Enabled = true; // Enable the timer
timer.Start(); // Start the timer
In the timer_tick handler, i want to delete the old log file, and create a new one with the same name, and continue recording logs. With:
void timer_Tick(object sender, EventArgs e)
{
//FileStream fs = File.Delete("C:\\" + filename + ".txt");
File.Delete("C:\\" + filename + ".txt");
FileStream fs2 = File.Create("C:\\" + filename + ".txt");
fs2.Close();
}
Syntactically there is no problem in the first iteration it's working perfectly but the problem is, after the first iteration of the timer, log.txt file becomes empty. Filestream can not write anything in it. I think there is a conflict between filestreams and streamwriter can you see any point that i couldnt see.
Thanks for reading, thanks in advance.