如何在stream.read中传递一个long作为偏移量?

mem*_*eme 3 c# stream

long bytesread = 0;
FileInfo f = new FileInfo(ficheiroaEnviar); 
tamanhoFicheiroEnviar = f.Length;
byte[] chunkFicheiro; 
while (!tudoFeito)
{
    long oQueFalta = (tamanhoFicheiroEnviar - bytesread);

    if (oQueFalta < tamanhoChunkPredefenido)
    {
        chunkFicheiro = new byte[tamanhoChunkPredefenido];
    }
    else
    {
        chunkFicheiro = new byte[oQueFalta];
    }

    using (FileStream fsSource = new FileStream(ficheiroaEnviar, FileMode.Open, FileAccess.Read))
    {
        lock (fsSource)
        {
            fsSource.Seek(bytesread, SeekOrigin.Begin);
            fsSource.Read(chunkFicheiro, bytesread, chunkFicheiro.Length);
        }
    }
    bytesread += chunkFicheiro.Length;
}
Run Code Online (Sandbox Code Playgroud)

我希望能够逐个读取 10GB 的文件?整数的最大数量为 2,147,483,647,10 GB 有 10 000 000 000 个字节。我如何使用 offset stream.read(other, int offset ,other) 以便它可以处理大文件?

(如果您愿意,可以编辑问题或标题)

bhm*_*ler 5

读取操作中的偏移值是读取操作在目标数组中的偏移量。从 fileStream 读取时,位置会随着读取而增加。https://msdn.microsoft.com/en-us/library/system.io.filestream.read(v=vs.110).aspx

offset 类型:System.Int32 数组中将放置读取字节的字节偏移量。

您的搜索操作将推进流(虽然它不应该被需要)

你的读取应该使用 0 作为偏移量,除非你想把它放在 chunkFicheiro 数组中的不同索引处

//Example of reading the file from the stream
do
{
    bytesRead = fsSource.Read(chunkFicheiro, 0, chunkFicheiro.Length);
    //Do something with the chunk if bytesRead > 0
} while (bytesRead > 0);
Run Code Online (Sandbox Code Playgroud)