Click here to Skip to main content
15,669,526 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to read files with high volume and the a variable Byte[] put.

Below code does not work
byte[] bytes = System.IO.File.ReadAllBytes(filename);


private byte [] StreamFile(string filename)
FileStream fs = new FileStream(filename, FileMode.Open,FileAccess.Read);

// Create a byte array of file stream length
byte[] ImageData = new byte[fs.Length];

//Read block of bytes from stream into the byte array

//Close the File Stream
return ImageData; //return the byte data


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

What I have tried:

How To Read Large File (Up 4 GB) And Convert To Byte?
Updated 27-Aug-16 2:05am
Henrik Jonsson 27-Aug-16 8:09am    
You should state what type of error (Exception) you get, to let us help you better.

Please observe that there is a 2G size limit of any .NET object, even on 64-bit systems. See
Patrice T 27-Aug-16 8:51am    
Read large file to do what ?

Basically, you can't, not with a byte array.
You can do it for objects larger than 2GB, if you are:
1) Running in 64bit mode on a 64 bit system. 32bit apps cannot address memory bigger than 2GB.
2) Are running .NET Framework V4.5 or greater.
3) Have set gcAllowVeryLargeObjects in your app.config: gcAllowVeryLargeObjects Element[^]
But ... array indexers are still limited to an integer - so you can't do this with byte arrays as your array index would be too big.
You can do it with a stream (~8TB is allowed) but not with an array.

And frankly, if you need to load a single file that large into memory, you probably need to look at how your app is working anyway because there is probably something very wrong with your basic algorithm...
Share this answer
mohsen_popo 27-Aug-16 8:39am    
An example is to view and download
OriginalGriff 27-Aug-16 8:49am    
"to view" - you are seriously considering viewing a 4GB image? Even if it was a bitmap (the least space efficient) that's still an image 32K by 32K: which would need approximately 128 4KTVs to display it...not the average browser window...
If you are talking about a video instead, then stream the damn thing - don't even try to load it!
mohsen_popo 27-Aug-16 9:02am    
If it's possible for step-by-step guidanceI will be grateful or introduce a valid source
OriginalGriff 27-Aug-16 9:56am    
Sorry, but you'd have to explain in a whole load more detail exactly what you are trying to do, including the environment you are working in and what you are trying to achieve!
mohsen_popo 27-Aug-16 10:02am    
Yes, I'm exactly the content needed
An example is to view and download
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900