title: Strange error: "The input stream is not a valid binary format"
body: <p>I have the following code to serialize and deserialize data:</p>
  
  <pre><code>    static public void Serialize(List&lt;Access&gt; accesos, Stream stream)
      {
          IFormatter formatter = new BinaryFormatter();
          formatter.Serialize(stream, accesos);
      }
  
      static public List&lt;Access&gt; Deserialize(Stream stream)
      {
          try
          {
              IFormatter formatter = new BinaryFormatter();
              List&lt;Access&gt; data = formatter.Deserialize(stream) as List&lt;Access&gt;;
              return data;
          }
          catch
          {
              return null;
          }
      }
  </code></pre>
  
  <p>The problem is that when I serialize an <code>List&lt;&gt;</code> to a file, and immediately try to deserialize, the error </p>
  
  <blockquote>
    <p>"The input stream is not a valid binary format"</p>
  </blockquote>
  
  <p>is thrown in <code>formatter.Deserialize(stream)</code> line.</p>
  
  <p>On serialization, the stream is being opened with:</p>
  
  <pre><code>Stream stream = File.Open(GetConfigurationFilePath(), FileMode.Create);
  </code></pre>
  
  <p>On deserializarion, the stream, is being opened with:</p>
  
  <pre><code>Stream stream = File.Open(GetConfigurationFilePath(), FileMode.Open);
  </code></pre>
  
  <p>What may be happening here? the binary format is not changed in any way.</p>
  
  <p>EDIT: This is how I call both static methods:</p>
  
  <pre><code>            using (Stream stream = File.Open(GetConfigurationFilePath(), FileMode.Create))
              {
                  this.Accesos = frm.Accesos;
                  Serializer.Serialize(this.Accesos, stream);
                  stream.Close();
              }
  
              using (Stream stream = File.Open(GetConfigurationFilePath(), FileMode.Open))
              {
                  this.Accesos = Serializer.Deserialize(stream);
                  stream.Close();
              }
  
      private string GetConfigurationFilePath()
      {
          string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
          if (path.Last() != '\\')
              path += '\\';
          path += CONFIG_FILE;
  
          return path;
      }
  </code></pre>