-2
FileStream fs = File.OpenRead(fullFilePath);
try
{
    Console.WriteLine("Read file size is : " + fs.Length);
    byte[] bytes = new byte[fs.Length]; //// **error this line**
    fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
    fs.Close();
    return bytes;
}
finally
{
    fs.Close();
}

read file size 2,885,760 KB. is error //

**Arithmetic operation resulted in an overflow.**

Veer
  • 1,575
  • 3
  • 16
  • 40
Satit Ongarj
  • 3
  • 1
  • 2

3 Answers3

6

That file size is over 2GB. The problem is that new byte[OverTwoBillionAndSome] is exceeding the limits. If the length was under 2GB this error would not occur (although it might still be advisable to not read it entirely into memory).

Consider streaming the data instead.

Community
  • 1
  • 1
Paul
  • 247
  • 1
  • 4
0
byte[] bytes = new byte[fs.Length];

2,885,760 is more than 2GB. Are you sure you have enough space in your RAM? Most probably not, that's why you are getting out of memory exception.

Even if you have enough space in RAM, a normal 32 Bit cannot have that much memory allocated to it

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
0

As Paul said, problem is the large size of file.

But with .NET Framework 4.5, you can use <gcAllowVeryLargeObjects> Element which supports you use objects that are greater than 2 GB in total size.

On 64-bit platforms, enables arrays that are greater than 2 gigabytes (GB) in total size.

You just need to change your configuration settings like;

<configuration>
  <runtime>
    <gcAllowVeryLargeObjects enabled="true" />
  </runtime>
</configuration>
Community
  • 1
  • 1
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364