Domanda

I dont know what I'm doing wrong here. my code works as expected on the first pass but on the second pass it wont run and throws an exception that just says "Reading would overrun buffer%"

I gather I'm doing something wrong with buffer and it doesn't like me overwriting it?

I had essentially the same code and changed it to match the msdn example but no joy..

using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
    byte[] buffer = new byte[1024];

    int bytesRead = 0;
    long bytesToRead = stream.Length;

    while (bytesToRead > 0)
    {               
        int n = stream.Read (buffer, bytesRead, 1024);
        if (n == 0) break;

        //do work on buffer...

        bytesRead += n;
        bytesToRead -= n;
    }
}   

Working in .Net 2.0 Mono.

È stato utile?

Soluzione

Stream.Read - second parameter is offset in the buffer and should be 0 in your case.

Altri suggerimenti

Read parameters is for buffer informations, you stock the data in buffer from offset to offset+nLength To advance in your file set stream.Position = bytesRead;

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top