How to append data to a binary file?

private static void AppendData(string filename, int intData, string stringData, byte[] lotsOfData)
{
    using (var fileStream = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None))
    using (var bw = new BinaryWriter(fileStream))
    {
        bw.Write(intData);
        bw.Write(stringData);
        bw.Write(lotsOfData);
    }
}

You should be able to do this via the Stream:

using (FileStream data = new FileStream(path, FileMode.Append))
{
    data.Write(...);
}

As for considerations - the main one would be: does the underlying data format support append? Many don't, unless it is your own raw data, or text etc. A well-formed xml document doesn't support append (without considering the final end-element), for example. Nor will something like a Word document. Some do, however. So; is your data OK with this...

Tags:

C#

.Net