Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a "2.17 GB" file and at the time of conversion it gives error.Non-negative number required.Parameter name: count. I know because the file's byte is too large for integer. But How to solve this?

OpenFileDialog f = new OpenFileDialog();
f.ShowDialog();
if (f.FileName != null && f.FileName != "")
{
        string filePath = f.FileName;
        string connectionString = Program.connectionString;
        FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
        BinaryReader reader = new BinaryReader(stream);
        byte[] file = reader.ReadBytes((int)stream.Length);//This line gives error
        reader.Close();
        stream.Close();
}
Posted
Comments
Richard MacCutchan 1-Mar-14 7:19am    
Why would you even try to read 2.17 Gbytes into memory in one go? That is not the way to process large files.

1 solution

You can't.
There is a fundamental limit on the size of any one object in .NET: 2 GB.
It doesn't matter if you run a 32 bit system or 64, no one single object can exceed 2GB.
Add to that when you convert a long to an int, 2GB is 80000000 hex, which will overflow an int and go negative, and you start to get real problems!

You will have to find some way to manipulate the file that doesn't involve reading the whole file at a time, and work with smaller chunks.
 
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