我正在使用以下代码尝试从 UNC 路径将大文件 (280Mb) 读入字节数组
public void ReadWholeArray(string fileName, byte[] data)
{
int offset = 0;
int remaining = data.Length;
log.Debug("ReadWholeArray");
FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
while (remaining > 0)
{
int read = stream.Read(data, offset, remaining);
if (read <= 0)
throw new EndOfStreamException
(String.Format("End of stream reached with {0} bytes left to read", remaining));
remaining -= read;
offset += read;
}
}
由于以下错误而爆炸。
System.IO.IOException: Insufficient system resources exist to complete the requested
如果我使用本地路径运行它,它工作正常,在我的测试用例中,UNC 路径实际上指向本地框。
有什么想法吗?
最佳答案
我怀疑下面的东西正在尝试读入另一个缓冲区,并且一次读取所有 280MB 都失败了。网络情况下可能需要比本地情况更多的缓冲区。
我会一次阅读大约 64K,而不是试图一次阅读全部内容。这足以避免分块带来的过多开销,但会避免需要巨大的缓冲区。
就个人而言,我倾向于只读到流的末尾,而不是假设文件大小将保持不变。参见 this question获取更多信息。
关于c# - IOException 使用 .NET 将大文件从 UNC 路径读取到字节数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/386088/