26

I have a binary file to which I want to append a chunk of data at the end of the file, how can I achieve this using C# and .net? Also is there anything to consider when writing to the end of a binary file? Thanks a lot for your help.

dodekja
  • 537
  • 11
  • 24
ryudice
  • 36,476
  • 32
  • 115
  • 163

3 Answers3

29
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);
    }
}
Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
  • 1
    +1 I like this answer because it shows you how to additionally shove native data types into the stream as binary representations. – John K Mar 07 '10 at 23:56
  • 2
    @jdk - which of course makes a *lot* of presumptions about what the underlying data *is*; a `byte[]` sure, that would always make sense... but the encoding for the others could be *miles* out. – Marc Gravell Mar 08 '10 at 05:22
  • 1
    +1 for the binary writer, if you are writing binary data it is always best to use the writer – Grant Peters Mar 08 '10 at 08:00
  • just wondering what would be the use case for appending binary data to the end of a file?Taking successive memory dumps maybe? – Divij Sehgal Jul 12 '17 at 17:54
  • You might want to put your comment on the original question as the poster seemed to have a use case in mind. – Jesse C. Slicer Jul 12 '17 at 18:07
  • 1
    My use case for this is I've got code that downloads large files in parts over multiple threads and I need to reassemble the parts into a single file. (The hosting server limits speed per connection, so multiple connections are my workaround to get the files faster.) – Benjamin Ray Jul 03 '19 at 22:25
6

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

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

Using StreamWriter and referencing DotNetPerls, make sure to add the True boolean to the StreamWriter constructor, if otherwise left blank, it'll overwrite as usual:

using System.IO;

class Program
{
    static void Main()
    {
    // 1: Write single line to new file
    using (StreamWriter writer = new StreamWriter("C:\\log.txt", true))
    {
        writer.WriteLine("Important data line 1");
    }
    // 2: Append line to the file
    using (StreamWriter writer = new StreamWriter("C:\\log.txt", true))
    {
        writer.WriteLine("Line 2");
    }
    }
}

Output
    (File "log.txt" contains these lines.)

Important data line 1
Line 2

This is the solution that I was actually looking for when I got here from Google, although it wasn't a binary file though, hope it helps someone else.

TankorSmash
  • 12,186
  • 6
  • 68
  • 106