Tutorial :How to serialize on to existing file?


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?


First, if what you really need is to store an array of object - put them in a container and serialize the container, as said earlier.

Now, if you really want to store two serialized object in a single file concatenated:

I'm not sure that this is possible "out of the box", but you can do two things.

  1. Use, or invent your own semi-tar file format, in which you'll write a small header in the beginning (or end) of the file that describes the content of the file (#'objects, and sizes).

  2. Write a C# code to detect the beginning and end of each serialized object yourself. I'm not entirely sure it's possible. Here is a non-official documentation of the serialization format. It contains header, however there's no guarantee that this header won't appear in the file.


This 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.


Put your objects into a collection object and serialize like that. If you'd manage to append a binary representation of an object at the end of an existing file you'd get problems reading it back.


Actually, if you just open the stream for writing, and positioning the pointer at the end of the stream, you can serialize another object and it will be appended to the existing stream.

Deserializing will also read past the object, but not further.

Obviously, if you want random access to your objects (deserialize object nr. 17, but not everything before it), then you need an index of the positions where each object starts, but if you just want to read all objects from the start, you can just serialize them out after each other.


How's about this

Deserialise the contents of your first file into an object in memory, add the first object to a collection (e.g. List<YourObjectType>), add your new object to that collection, then serialise the collection (containing two objects now) on top of the existing file.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »