title: How can I write to a file instead of an in memory byte array?
body: <p>At the moment I have this code, which recieves information over the network, stores it into an in-memory byte array, and then writes it out via a FileStream.</p>
  
  <pre><code> StreamReader reader = new StreamReader(tcpClient.GetStream());
  
    // first message is file size
           string cmdFileSize = reader.ReadLine();
  
    // first message is filename
          string cmdFileName = reader.ReadLine();
  
          int length = Convert.ToInt32(cmdFileSize);
          byte[] buffer = new byte[length];
          int received = 0;
          int read = 0;
          int size = 1024;
          int remaining = 0;
  
  
     while (received &lt; length)
          {
              remaining = length - received;
              if (remaining &lt; size)
              {
                  size = remaining;
              }
  
              read = tcpClient.GetStream().Read(buffer, received, size);
              received += read;
              if (read &lt; size)
              {
                  break;
              }
          }    
  </code></pre>
  
  <p>After this, "buffer" is written to a file via FileStream.  This works well, but when I have files that are large, obviously that amount of memory gets taken up.  I'd like to stream these files directly to disk, instead of writing to ram and then rewriting them to the hard drive. </p>
  
  <p>How could I do this?</p>