title: Sharing violation IOException while reading and writing to file C#
body: <p>Here is my code:</p>
  
  <pre><code>public static TextWriter twLog = null;
  private int fileNo = 1;
  private string line = null;
  
  TextReader tr = new StreamReader("file_no.txt");
  TextWriter tw = new StreamWriter("file_no.txt");
  line = tr.ReadLine();
  if(line != null){
      fileNo = int.Parse(line);
      twLog = new StreamWriter("log_" + line + ".txt");
  }else{
      twLog = new StreamWriter("log_" + fileNo.toString() + ".txt");  
  }
  System.IO.File.WriteAllText("file_no.txt",string.Empty);
  tw.WriteLine((fileNo++).ToString());
  tr.Close();
  tw.Close();
  twLog.Close();
  </code></pre>
  
  <p>It throws this error:</p>
  
  <blockquote>
    <p>IOException: Sharing violation on path C:\Users\Water Simulation\file_no.txt</p>
  </blockquote>
  
  <p>What i'm trying to do is just open a file with log_x.txt name and take the "x" from file_no.txt file.If file_no.txt file is empty make log file's name log_1.txt and write "fileNo + 1" to file_no.txt.After a new program starts the new log file name must be log_2.txt.But i'm getting this error and i couldn't understand what am i doing wrong.Thanks for help.</p>