Memorystream.Read()总是返回0 bytesRead,空字节[]

Use*_*999 5 .net c# memorystream

我现在有一个的MemoryStream约为长度30000(Named memStream here)我希望此的MemoryStream中chunks使用下面的代码(I拾起的净和改性有点):

        byte[] chunk = new byte[4096];
        bool hasNext = true;

        while(hasNext)
        {
            int index = 0;

            while (index < chunk.Length)
            {
                int bytesRead = memStream.Read(chunk, index, chunk.Length - index);
                if (bytesRead == 0)
                {
                    break;
                }
                index += bytesRead;
                //Do something with this chunk
            }
            if (index != 0) // Our previous chunk may have been the last one
            {
                //Do something with the last chunk
            }
            if (index != chunk.Length) // We didn't read a full chunk: we're done
            {
                hasNext = false;
            }
        }
Run Code Online (Sandbox Code Playgroud)

但是以下read()方法似乎不起作用

  int bytesRead = memStream.Read(chunk, index, chunk.Length - index);

  WHERE
    chunk: new byte[4096]
    index: 0
    memstream: capacitiy & length : 34272
    memstream: position 0 (according to VS watch)

 Always returns
    0 bytesRead
    Chunk with all values containing '0'
Run Code Online (Sandbox Code Playgroud)

知道为什么吗?这可能是权利许可吗?

感谢您的时间.

gil*_* kr 6

在创建并填充MemoryStream之后,您需要将读取位置设置为开头,如下所示:

memStream.Seek(0, SeekOrigin.Begin);
Run Code Online (Sandbox Code Playgroud)