title: Writing from a MemoryStream to FileStream corrupts the data
body: <p>I'm trying to write a byte array to a file, which, I would assume, should be simple. I'm trying to use a <code>MemoryStream</code> to get the <code>byte()</code> and copy it to the <code>FileStream</code>. See my code below.                      </p>
  
  <pre><code>If File.Exists(path) Then
      Dim sr As StreamReader = New StreamReader(path)
      Do While sr.Peek() &gt;= 0
          sr.Read()
      Loop
      sr.Close()
  
  Else
      res = GetInfo(timeout, bytes)
      If res = 0 Then
          Dim s As Stream = New FileStream(path, FileMode.Create, FileAccess.ReadWrite)
          Dim stream As New MemoryStream(bytes)
  
          stream.CopyTo(s)
          stream.Dispose()
  
         ' do other stuff
      End If
  End If
  </code></pre>
  
  <p>It compiles fine but when I check the written file, the data is corrupted and looks like:</p>
  
  <pre><code>Í¤I¶µ&amp;Ôœ¸Šíd9ïè´5ì#~¿g•ø9£ÓÙ=¸Š€8j]f½’˜»4e18+jš³VÀú¸àc?ê
  </code></pre>
  
  <p>Other things I've tried:</p>
  
  <ul>
  <li>Using a <code>StreamWriter</code>. It works fine, however, I read here that it should only be used for <code>String</code>s and not <code>byte</code>s, so I'm not using it.</li>
  <li>Using <code>FileStream.Write</code> method, the data is corrupted again</li>
  <li>Using <code>MemoryStream.WriteTo</code> method, the data is corrupted again</li>
  </ul>
  
  <p>The byte array looks like:</p>
  
  <pre><code>105
  203
  13
  47
  ...
  </code></pre>
  
  <p>My data is actually a <code>Byte()</code> but gets corrupted with <code>Byte</code> methods and works with the <code>String</code> method, so it's kind of confusing. Am I missing a step or something?</p>