【问题标题】:Generate Byte Array from Stream从 Stream 生成字节数组
【发布时间】:2014-05-13 04:26:55
【问题描述】:

我正在尝试从“.rtf”文件流中生成字节数组。 代码如下:

Public Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Try
        Dim result As System.Nullable(Of Boolean) = textDialog.ShowDialog()

        If result = True Then
            Dim fileStream As Stream = textDialog.OpenFile()

            GetStreamAsByteArray(fileStream)
        End If
    Catch ex As Exception

    End Try

End Sub

Private Function GetStreamAsByteArray(ByVal stream As System.IO.Stream) As Byte()

    Dim streamLength As Integer = Convert.ToInt32(stream.Length)

    Dim fileData As Byte() = New Byte(streamLength) {}

    ' Read the file into a byte array
    stream.Read(fileData, 0, streamLength)
    stream.Flush()
    stream.Close()

    Return fileData

End Function

上面的代码为打开的文件生成流长度,但是返回的字节数组在数组中只有 0。 如何生成正确的字节数组?

【问题讨论】:

    标签: vb.net silverlight bytearray filestream


    【解决方案1】:

    您的函数不会将字节数组返回给任何对象。这个例子对我有用:

     Dim bytes = GetStreamAsByteArray(textDialog.File.OpenRead)
     MessageBox.Show(bytes.Length.ToString)
    

    【讨论】:

    • textDialog.File.OpenRead 不可用
    • 我在 Silverlight 中进行了测试,您使用的是什么版本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 2018-06-25
    • 2020-01-04
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多