54

I would like to append a byte array to an already existing file (C:\test.exe). Assume the following byte array:

byte[] appendMe = new byte[ 1000 ] ;

File.AppendAllBytes(@"C:\test.exe", appendMe); // Something like this - Yes, I know this method does not really exist.

I would do this using File.WriteAllBytes, but I am going to be using an ENORMOUS byte array, and System.MemoryOverload exception is constantly being thrown. So, I will most likely have to split the large array up into pieces and append each byte array to the end of the file.

Thank you,

Evan

  • I don't understand the problem. – Yuriy Faktorovich Jul 28 '11 at 16:32
  • 1
    My main goal is to add two enormous byte arrays together. This is not possible, however, as an exception (out of memory) is thrown. So, my solution is to write each byte array to an output file separately, (as opposed to combining them and writing them to the file as one). –  Jul 28 '11 at 16:34
  • You should at least provide some code that you've tried... So far it is unclear what is causing your problem - all Stream and Writer classes are able to write byte arrays directly. – Alexei Levenkov Jul 28 '11 at 16:38
  • 18
    What is so difficult to understand about "append a byte array to an already existing file" ? Everyone else seemed to understand it... –  Jul 28 '11 at 16:41

5 Answers5

87

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);
    }
}
Ani
  • 111,048
  • 26
  • 262
  • 307
  • 4
    This looks perfect. Some other answers use BinaryWriter; why would one choose this over the FileStream method? I'm guessing it performs exactly the same function, but takes more lines of code. – radsdau Mar 30 '16 at 22:53
  • 2
    does it need to flush? – John Demetriou Jul 18 '16 at 12:56
  • 1
    @JohnDemetriou after you get out of the block from the using statement, Close() gets called, which will in turn will call Flush(); so I don't think you need to call Flush() – Goku Feb 18 '20 at 22:55
6

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.

Hamed
  • 1,175
  • 3
  • 20
  • 46
  • 7
    As far as I can tell this is a VB.Net only solution. https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.filesystem.writeallbytes(v=vs.110).aspx – Richard Barker Jun 29 '16 at 22:21
6
  1. Create a new FileStream.
  2. Seek() to the end.
  3. Write() the bytes.
  4. Close() the stream.
user541686
  • 205,094
  • 128
  • 528
  • 886
  • This seems to be the best solution to my problem. I'll give it a go and let you know. Thanks –  Jul 28 '11 at 16:36
3

I'm not exactly sure what the question is, but C# has a BinaryWriter method that takes an array of bytes.

BinaryWriter(Byte[])

bool writeFinished = false;
string fileName = "C:\\test.exe";
FileStream fs = new FileString(fileName);
BinaryWriter bw = new BinaryWriter(fs);
int pos = fs.Length;
while(!writeFinished)
{
   byte[] data = GetData();
   bw.Write(data, pos, data.Length);
   pos += data.Length;
}

Where writeFinished is true when all the data has been appended, and GetData() returns an array of data to be appended.

user807566
  • 2,828
  • 3
  • 20
  • 27
0

you can simply create a function to do this

public static void AppendToFile(string fileToWrite, byte[] DT)
{
    using (FileStream FS = new FileStream(fileToWrite, File.Exists(fileToWrite) ? FileMode.Append : FileMode.OpenOrCreate, FileAccess.Write)) {
        FS.Write(DT, 0, DT.Length);
        FS.Close();
    }
}
Jack Gajanan
  • 1,596
  • 14
  • 18