-1
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.**

3
  • 3
    This doesn't make sense at all. Please put some more effort into improving your question. Commented Jun 19, 2013 at 5:16
  • 3
    stackoverflow.com/a/3944336/251311 PS: and please try to use google before you ask next time Commented Jun 19, 2013 at 5:17
  • 1
    Btw, why do you need to read multy-gigabyte files into the memory? Commented Jun 19, 2013 at 5:18

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.