3

I am trying to generate byte array from a stream of ".rtf" file. The code is as follows:

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

The above code generates stream length for the file opened however the byte array returned only have 0's in the array. How can i generate correct byte array?

1 Answer 1

1

You function does not returns the byte array to any object. This example works for me:

 Dim bytes = GetStreamAsByteArray(textDialog.File.OpenRead)
 MessageBox.Show(bytes.Length.ToString)
Sign up to request clarification or add additional context in comments.

2 Comments

textDialog.File.OpenRead is not available
I ran a test in Silverlight, what version are you using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.