I have a web server which will read large binary files (several megabytes) into byte arrays. The server could be reading several files at the same time (different page requests), so I am looking for the most optimized way for doing this without taxing the CPU too much. Is the code below good enough?
public byte FileToByteArray(string fileName)
byte buff = null;
FileStream fs = new FileStream(fileName,
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(fileName).Length;
buff = br.ReadBytes((int) numBytes);
Simply replace the whole thing with:
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.