What is the most efficient way to save a byte array as a file on disk in C#?

Solution 1:

That would be File.WriteAllBytes().

Solution 2:

System.IO.File.WriteAllBytes(path, data) should do fine.

Solution 3:

And WriteAllBytes just performs

using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
{
    stream.Write(bytes, 0, bytes.Length);
}

BinaryWriter has a misleading name, it's intended for writing primitives as a byte representations instead of writing binary data. All its Write(byte[]) method does is perform Write() on the stream its using, in this case a FileStream.

Solution 4:

Not sure what you mean by "efficient" in this context, but I'd use System.IO.File.WriteAllBytes(string path, byte[] bytes) - Certainly efficient in terms of LOC.

Solution 5:

I had a similar problem dumping a 300 MB Byte array to a disk file...
I used StreamWriter, and it took me a good 30 minutes to dump the file.
Using FilePut took me arround 3-4 minutes, and when I used BinaryWriter, the file was dumped in 50-60 seconds.
If you use BinaryWriter you will have a better performance.