【发布时间】:2018-10-29 19:45:01
【问题描述】:
我正在使用以下代码将长度byte[] val写入文件末尾,然后写入byte[] val本身
byte[] len = BitConverter.GetBytes((UInt16) val.Length);
int fileLen = (int)new FileInfo(filePath).Length;
using (Stream stream = File.OpenWrite(filePath))
{
stream.Write(len, fileLen, 2);
stream.Write(val, fileLen + 2, val.Length);
}
我在using 块的最后一行收到此错误:
偏移量和长度超出数组的范围或计数更大 比从索引到源末尾的元素数 收藏。
当我检查文件时,我发现流根本没有写入前 2 个字节,这就是发生错误的原因。为什么会这样?
【问题讨论】:
标签: c# .net stream runtime-error filestream