title: Writing to file using StreamWriter much slower than file copy over slow network
body: <p>I have a program that attempts to write quite a large amount of text to a file on a remote server overseas, which has a slow network connection.</p>
  
  <p>Using the following code, where <code>outputFileContent</code> is a <code>StringBuilder</code>:</p>
  
  <pre><code>using (var outfile = new StreamWriter(myRemoteFilePath))
  {
      outfile.Write(outputFileContent.ToString());
  }
  </code></pre>
  
  <p>it is taking a seriously long time to run (several minutes), whereas if I first write to a local file and then copy it across to the remote location, it is much quicker (20-30 secs):</p>
  
  <pre><code>string tempFilePath = Path.GetTempFileName();
  using (var outfile = new StreamWriter(tempFilePath))
  {
      outfile.Write(outputFileContent.ToString());
  }
  
  System.IO.File.Copy(tempFilePath, myRemoteFilePath, true)
  </code></pre>
  
  <p>Any idea why this is happening? My only guess is that it is something to do with buffering across the network, or perhaps because the stream writer doesn't know how big it needs to be ahead of time.</p>