Click here to Skip to main content
14,698,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I`am using the following code to serialize a list of bytes :
    FileStream fs = new FileStream("DataFile.dat", FileMode.Create);

    BinaryFormatter formatter = new BinaryFormatter();
    try
    {
        formatter.Serialize(fs, myList);
    }
    catch (Exception exc)
    {
        Console.WriteLine("Failed to serialize. Reason: " + exc.Message);
        throw;
    }
    finally
    {
        fs.Close();
    }

    SendFileToServer(username, "DataFile.dat", "DataFile.dat", null);
    MessageBox.Show("Trimiterea detali reusita");
}


then for deserialization I`am using the following code:

List<byte[]> myList = null;

FileStream fs = new FileStream("D:\\Server\\Rares\\DataFile.dat", FileMode.Open);
try
{
 
    BinaryFormatter formatter = new BinaryFormatter();
    myList = (List<byte[]>)formatter.Deserialize(fs);

}
catch (Exception exc)
{
    Console.WriteLine("Failed to deserialize. Reason: " + exc.Message);
    throw;
}
finally
{
    fs.Close();
}
Now my problem is that I am catching this exception:
Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.


and I don`t know why. If you have any idea where is my problem I will apreciate your help.
Thank you in advance for your help and If you need some more information aboat my code ask me .
Posted
Updated 25-Feb-12 10:56am
v2
Comments
R. Giskard Reventlov 25-Feb-12 19:01pm
   
If Microsoft wrote it, it must work!
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter(v=vs.100).aspx

1 solution

see this link below that is related to your question

http://forums.asp.net/t/1765346.aspx/1[^]

vote and accept solution
If this will help you
Thanks
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900