.net - 将内存流读入字节数组

标签 .net vb.net

我在以 block 的形式读取内存流时遇到一些问题。

Dim ByteBuffer(4096) As Byte

While ProcessedBytes < FileLength

    BytesRead = MemoryStream.Read(ByteBuffer, 0, 4096)
    'Write the buffer to an output stream
    ProcessedBytes += BytesRead

End While

“MemoryStream.Read(ByteBuffer, 0, 4096)”始终返回零。我查看了“MemoryStream.Length”,流中肯定有一些字节。

最佳答案

如果您只是想从 MemoryStream 中获取 byte[] 数据,则可以调用 MemoryStream.ToArray() :

Dim ByteBuffer() as Byte = MemoryStream.ToArray()

否则,请确保设置 MemoryStreamPosition在读取之前先将其设置为 0。

关于.net - 将内存流读入字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11956697/

相关文章:

c# - 如何使用 Mono 和 mkbundle 将一个简单的 .Net 控制台项目转换为可移植的 exe?

初始化类时相当于 VB.NET "With"语句的 Java

mysql - vb.net datagridview过滤列未运行

vb.net - 十六进制数(不是 ASCII 十六进制值)到 VB.NET 中的字符串

c# - 系统异常 : 'Cannot create a table without columns, 是否具有公共(public)属性?

.net - 选项卡容器 - 按钮事件未触发

c# - 如何以编程方式单击 WPF 中的按钮?

.NET 日期比较 : Count the amount of working days since a date?

mysql - 从两个表中检索数据时如何填充文本框?

asp.net - 根据 MS Access 查询的结果设置 gridview 列的值