title: Append to file using BinaryFormatter
body: <p>I'm using this code </p>
  
  <pre><code>    for (int i = 0; i &lt; 3; ++i)
      {
          List&lt;int&gt; tl = new List&lt;int&gt;();
          tl.Add(5);
          tl.Add(4);
          using (var fileStream = new FileStream(@"C:\file.dat", FileMode.Append))
          {
              var bFormatter = new BinaryFormatter();
              bFormatter.Serialize(fileStream, tl);
              //fileStream.Close();
          }
  
          var list = new List&lt;int&gt;();
  
          using (var fileStream = new FileStream(@"C:\file.dat", FileMode.Open))
          {
              var bFormatter = new BinaryFormatter();
              //while (fileStream.Position != fileStream.Length)
              //{
              //     list.Add((int)bFormatter.Deserialize(fileStream));
              //}
              list = (List&lt;int&gt;)bFormatter.Deserialize(fileStream);
              //fileStream.Close();
          }
      }
  </code></pre>
  
  <p>I expect .dat file will be </p>
  
  <blockquote>
    <p>5 4 5 4 5 4</p>
  </blockquote>
  
  <p>but it's only </p>
  
  <blockquote>
    <p>5 4</p>
  </blockquote>
  
  <p>also this code return also</p>
  
  <blockquote>
    <p>5 4</p>
  </blockquote>
  
  <pre><code>        List&lt;int&gt; tl = new List&lt;int&gt;();
          tl.Add(5);
          tl.Add(4);
          using (var fileStream = new FileStream(@"C:\file.dat", FileMode.Append))
          {
              var bFormatter = new BinaryFormatter();
              bFormatter.Serialize(fileStream, tl);
          }
  
          tl.Clear();
          tl.Add(3);
          tl.Add(2);
          using (var fileStream = new FileStream(@"C:\file.dat", FileMode.Append))
          {
              var bFormatter = new BinaryFormatter();
              bFormatter.Serialize(fileStream, tl);
          }
  
          var list = new List&lt;int&gt;();
  
          using (var fileStream = new FileStream(@"C:\file.dat", FileMode.Open))
          {
              var bFormatter = new BinaryFormatter();
              list = (List&lt;int&gt;)bFormatter.Deserialize(fileStream);
          }
  </code></pre>
  
  <p>it looks like it de-serialize only first portion that was appended.</p>
  
  <p>why data don't append?</p>
  
  <p><strong>UPDATE:</strong>
  So the solution is:</p>
  
  <pre><code>        var list = new List&lt;int&gt;();
  
          using (var fileStream = new FileStream(@"C:\file.dat", FileMode.Open))
          {
              var bFormatter = new BinaryFormatter();
              while (fileStream.Position != fileStream.Length)
              {
                  var t = (List&lt;int&gt;)(bFormatter.Deserialize(fileStream));
                  list.AddRange(t);
              }
          }
  </code></pre>