c#2中的字节数组内存不足

-2 c#

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();
}
Run Code Online (Sandbox Code Playgroud)

read file size 2,885,760 KB.是错误//

**Arithmetic operation resulted in an overflow.**

Pau*_*aul 6

该文件大小超过2GB.问题是,new byte[OverTwoBillionAndSome]超过极限.如果长度低于 2GB,则不会发生此错误(尽管可能仍然不建议将其完全读入内存).

考虑改为流式传输数据.