I have a file with an object serialized with System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter. To ensure that the file is written properly, I append a 64 character checksum to the end. I can pull out and verify the checksum, but I can't figure out how to get the BinaryFormatter to deserialize the data. I can't have it deserialize the file stream, because it has the checksum at the end.
If I:
- Open the file
- Read it with a StreamReader
- Output the stream to a string
- Remove the checksum
- Convert the string to a byte[] using System.Text.Encoding.Ascii.GetBytes()
- Put the bytes into a MemoryStream
- Deserialize the MemoryStream
I get this error:
Code:
System.Runtime.Serialization.SerializationException:
Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.