title: C# File Stream Conflicts with Timer
body: <p>I'm coding a program for recording security logs in c#. But there are several problems. </p>
  
  <p>First i create a .txt file to record logs. With:   </p>
  
  <pre><code>FileStream fs1 = File.Create("C:\\" + filename + ".txt");
  fs1.Close();
  </code></pre>
  
  <p>Then, <strong>in a loop</strong> i write something in the txt file with:</p>
  
  <pre><code>TextWriter tsw1 = new StreamWriter(@"C:\\" + filename + ".txt", true);
  //Writing text to the file.
  tsw1.WriteLine(curLogs + " \n");
  //Close the file.
  tsw1.Close();
  </code></pre>
  
  <p>After that, I set a timer in c# and every -for example- 10 mins i want to reset log file.</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Thanks for reading, thanks in advance.</p>