title: C# Overwriting file with StreamWriter created from FileStream
body: <p>I need to manipulate the contents of a file:</p>
  
  <pre><code> FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);               
   StreamReader sr = new StreamReader(fs);
   StreamWriter sw = new StreamWriter(fs);
   newString = someStringTransformation(sr.ReadToEnd());
   sw.Write(newString);
   fs.flush();
   fs.Close();
  </code></pre>
  
  <p>However the above appends the newString instead of overwriting the file with the new changes. It needs to be done so that no other application can access the file in between reading a writing which is why I'm creating the reader and writer from a FileStream object.</p>
  
  <p>I know that you can create a StreanWriter with the second parameter set to false as described <a href="https://stackoverflow.com/questions/6445125/how-to-overwrite-data-in-a-txt-file">here</a>. However when creating the StreamWriter as above that does not seem to be one of the parameters.</p>