Best way to read a large file into a byte array in C#? Best way to read a large file into a byte array in C#? arrays arrays

Best way to read a large file into a byte array in C#?


Simply replace the whole thing with:

return File.ReadAllBytes(fileName);

However, if you are concerned about the memory consumption, you should not read the whole file into memory all at once at all. You should do that in chunks.


I might argue that the answer here generally is "don't". Unless you absolutely need all the data at once, consider using a Stream-based API (or some variant of reader / iterator). That is especially important when you have multiple parallel operations (as suggested by the question) to minimise system load and maximise throughput.

For example, if you are streaming data to a caller:

Stream dest = ...using(Stream source = File.OpenRead(path)) {    byte[] buffer = new byte[2048];    int bytesRead;    while((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) {        dest.Write(buffer, 0, bytesRead);    }}


I would think this:

byte[] file = System.IO.File.ReadAllBytes(fileName);