6
我爬循環的第二次迭代以下錯誤:
Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
讀取文件錯誤到數組
,這是我的循環
FileStream fs = new FileStream("D:\\06.Total Eclipse Of The Moon.mp3", FileMode.Open);
byte[] _FileName = new byte[1024];
long _FileLengh = fs.Length;
int position = 0;
for (int i = 1024; i < fs.Length; i += 1024)
{
fs.Read(_FileName, position, Convert.ToInt32(i));
sck.Client.Send(_FileName);
Thread.Sleep(30);
long unsend = _FileLengh - position;
if (unsend < 1024)
{
position += (int)unsend;
}
else
{
position += i;
}
}
fs.Close();
}
fs.Length = 5505214
fs.Read(_filename,位置,2048); << position = 1024,2048 - 1024 = 1024所以我的_FileName = 1024有足夠的空間 –
@Acid:但是你說你想開始讀取_FileName的索引1024。有*是*沒有這樣的索引 - 數組的最後一個索引是1023.請閱讀'Stream.Read'的文檔 - 我不認爲你明白第二個和第三個參數是什麼。 –
最後一句話實際上讓我注意到我錯誤地解釋了'offset'參數。認爲它與源流有關,而不是緩衝區。幫助我修復一個重要的bug,謝謝! – wodzu