title: BinaryWriter in loop causes "file is being used by another process" error even though it is closed and 'using'
body: <p>Title sais it all really.</p>
  
  <pre><code>private bool addToBinary(byte[] msg, string filepath)
  {
      bool succ = false;
  
      do
      {
          try
          {
              using (Stream fileStream = new FileStream(filepath, FileMode.Append, FileAccess.Write, FileShare.None))
              {
                  using (BinaryWriter bw = new BinaryWriter(fileStream))
                  {
                      bw.Write(msg);
  
                      bw.Flush();
                      fileStream.Flush();
                      bw.Close();
                  }
              }
              succ = true;
          }
          catch (IOException ex) { Console.WriteLine("Write Exception (addToBinary) : " + ex.Message); }
          catch (Exception ex) { Console.WriteLine("Some Exception occured (addToBinary) : " + ex.Message); return false; }
      } while (!succ);
      return true;
  }
  </code></pre>
  
  <p>(bw.close also closes the underlying stream)</p>
  
  <p>Using this in any loop causes an output such as;</p>
  
  <pre><code>A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
  Write Exception (addToBinary) : The process cannot access the file 'C:\test\test.png' because it is being used by another process.
  </code></pre>
  
  <p>The bigger the file gets, the more of these errors pop up. It does get through eventually but it significantly reduces file writing speed. It's the <code>Stream fileStream =</code> bit that causes the exception.</p>
  
  <p>What did I do wrong?</p>
  
  <p>Example usage;</p>
  
  <pre><code>do
  {
      serverStream = clientSocket.GetStream();
      bytesRead = serverStream.Read(inStream, 0, buffSize); //How many bytes did we just read from the stream?
  
      recstrbytes = new byte[bytesRead]; //Final byte array
      Array.Copy(inStream, recstrbytes, bytesRead); //Copy from inStream to the final byte array
      addToBinary(recstrbytes, @"C:\test\test.png"); //Append final byte array to binary
      received += recstrbytes.Length; //Increment bytes received
  }while (received &lt; filesize);
  </code></pre>