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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With