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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…