FileStream.Read() 不适用于 c# 中大于 0 的偏移量

FileStream.Read() doesn't work with offset more than 0 in c#

我正在 c# 中开发文件传输系统。客户端将文件读入分区并将每个分区发送到服务器。

每个分区都是 4096 字节,除了最后一个分区是其余字节。数据通过 TCP 套接字发送。

问题出在 FileStream.Read() 函数中,因为只要 offset 大于 0,函数就会引发 System.ArgumentException。这是代码:

string fileSize = "0";
FileStream f = null;
try
{
    f = File.Open(path, FileMode.Open, FileAccess.Read);
    fileSize = f.Length.ToString();
}
catch (Exception e)
{ }
// send partition count to server
int partitionCount = int.Parse(fileSize) / 4096;
int lastPartitionSize = (int.Parse(fileSize) - (partitionCount * 4096));
Thread.Sleep(20);
sendData(partitionCount.ToString());
Thread.Sleep(20);
sendData(lastPartitionSize.ToString());
Thread.Sleep(20);
for (int i = 0; i < (partitionCount); i++)
{
    byte[] data = new byte[4096];
    int offset = (4096 * i);
    Console.WriteLine("Partition: " + (i+1) + "  |  Offset: " + offset + "  |  Bytes Left: " + (f.Length - (4096*i)));
    f.Read(data, offset, 4096); // problem is RIGHT HERE
    sendRawData(data);
}
byte[] lastData = new byte[lastPartitionSize];
f.Read(lastData, (4096 * partitionCount), lastPartitionSize);
sendRawData(lastData);

来自 MSDN:

offset

The byte offset in array at which the read bytes will be placed.

因此您的偏移量应始终为零