Let say I have a file that contains a serialized object by BinaryFomatter. Now I want to be able to serialize another object and APPEND this on that existing file.
How can I do it?
See Question&Answers more detail:osLet say I have a file that contains a serialized object by BinaryFomatter. Now I want to be able to serialize another object and APPEND this on that existing file.
How can I do it?
See Question&Answers more detail:osThis is indeed possible. The code below appends the object.
using (var fileStream = new FileStream("C:file.dat", FileMode.Append))
{
var bFormatter = new BinaryFormatter();
bFormatter.Serialize(fileStream, objectToSerialize);
}
The following code de-serializes the objects.
var list = new List<ObjectToSerialize>();
using (var fileStream = new FileStream("C:file.dat", FileMode.Open))
{
var bFormatter = new BinaryFormatter();
while (fileStream.Position != fileStream.Length)
{
list.Add((ObjectToSerialize)bFormatter.Deserialize(fileStream));
}
}
Note for this to work the file must only contain the same objects.