Click here to Skip to main content
15,564,889 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi.
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);

OR

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
fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length));

//Close the File Stream
fs.Close();
return ImageData; //return the byte data
}

OR

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?
Posted
Updated 27-Aug-16 3:05am
Comments
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 http://stackoverflow.com/questions/1087982/single-objects-still-limited-to-2-gb-in-size-in-clr-4-0?noredirect=1&lq=1
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.
And
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
 
Comments
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