首页 > 代码库 > Stream Read

Stream Read

看过很多回 关于 stream read(byte[] buffer, int offset, int count)

有个问题一直没注意过

offset + count 不能超过 buffer的长度

所以

FileStream stream = new FileStream(txtAttachments.Text, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);            byte[] bytes = new byte[stream.Length];            int numRead = 0;            int count = 50 > bytes.Length ? bytes.Length : 50;            int numToRead = bytes.Length;            int offset = 0;            while (numToRead > 0)            {                numRead = stream.Read(bytes, offset, count);                if (numRead == 0)                    break;                numToRead = numToRead - numRead;                offset = offset + numRead;                if (numToRead < count)                    count = numToRead;            }                        stream.Close();

有个问题没想明白,如果文件长度1000, 我请求900,难道它会只返回700?