2014-04-02 34 views
1

我想從「.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的在數組中。 我怎樣才能生成正確的字節數組?

回答

0

您的函數不會將字節數組返回給任何對象。這個例子對我的作品:

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

textDialog.File.OpenRead不可用 – Anish

+0

我,你使用的是什麼版本運行在Silverlight的測試? – OneFineDay

+0

'textDialog'是一個'OpenFileDialog'嗎? – OneFineDay