56

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

4
  • I don't understand the problem. Commented Jul 28, 2011 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). Commented Jul 28, 2011 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. Commented Jul 28, 2011 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... Commented Jul 28, 2011 at 16:41

6 Answers 6

95

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);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
does it need to flush?
@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()
8
  1. Create a new FileStream.
  2. Seek() to the end.
  3. Write() the bytes.
  4. Close() the stream.

1 Comment

This seems to be the best solution to my problem. I'll give it a go and let you know. Thanks
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.

1 Comment

As far as I can tell this is a VB.Net only solution. msdn.microsoft.com/en-us/library/…
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.

Comments

2

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();
    }
}

1 Comment

using{} does not need calling FS.Close();
0

An alternative approach using MemoryStream:

public static byte[] GetLargeBytes(byte[] bytes)
{
    using (var stream = new MemoryStream(new byte[1024 * 1024 * 10], true))
    {
        stream.Write(bytes);
        stream.Position = 0;
        return stream.ToArray();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.