title: Read, delete, and write to file while locking via FileShare FileShare.None?
body: <p>This may have been asked before, so I apologize in advance. However, I felt that there are enough pieces here unique to my problem that I wasn't finding that it warranted the question.</p>
  
  <p>I am trying to open a file using the <code>FileStream</code> method. I need to have this file opened under lock from outside read/write, which is why I used the <code>FileShare.None</code> attribute while opening it. Once it is open and locked, I read the contents of the file into a string array line by line. I change one of the lines based on information that needs to be updated, and then I write the lines back to the file.</p>
  
  <p>The problem I am having is that the written lines are appended after the original lines that are read. I need to wipe the file once I have read in from it so that what I write back are the only things in the file.</p>
  
  <p>I am confused because I don't want to close the <code>FileStream</code> and reopen it for write only (which should clear the file), because that would release the lock I have on the file.</p>
  
  <pre><code>FileStream fs = new FileStream("trains.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);
  StreamReader sr = new StreamReader(fs);
  StreamWriter sw = new StreamWriter(fs);
  
  string[] lines = new string[trains.Length];
  
  //Loop through all lines in the file
  for (int i = 0; i &lt; lines.Length; i++)
  {
      lines[i] = sr.ReadLine();
  
      /*If the ID of the train in the file matches the parameter, then
          set the line to the InfoLine of the train.*/
      if (lines[i][0].ToString() == train.ID().ToString())
      {
          lines[i] = train.GetInfoLine();
      }
  }
  
  //Write back the lines to the file
  for (int i = 0; i &lt; lines.Length; i++)
  {
      sw.WriteLine(lines[i]);
  }
  
  sw.Close();
  sr.Close();
  fs.Close();
  </code></pre>
  
  <p>In the above code, trains.Length is simply the length of an array of class objects. <code>GetInfoLine()</code> is a method I have that returns a string of information I want to write to the file.</p>