문제

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.

도움이 되었습니까?

해결책

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

다른 팁

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;

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top