Reputation: 1960
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?
Upvotes: 3
Views: 11697
Reputation: 9024
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)
Upvotes: 1