C# Append byte array to existing file C# Append byte array to existing file arrays arrays

C# Append byte array to existing file


One way would be to create a FileStream with the FileMode.Append creation mode.

Opens the file if it exists and seeks to the end of the file, or creates a new file.

This would look something like:

public static void AppendAllBytes(string path, byte[] bytes){    //argument-checking here.    using (var stream = new FileStream(path, FileMode.Append))    {        stream.Write(bytes, 0, bytes.Length);    }}


  1. Create a new FileStream.
  2. Seek() to the end.
  3. Write() the bytes.
  4. Close() the stream.


You can also use the built-in FileSystem.WriteAllBytes Method (String, Byte[], Boolean).

public static void WriteAllBytes(    string file,    byte[] data,    bool append)

Set append to True to append to the file contents; False to overwrite the file contents. Default is False.