【发布时间】:2015-07-02 16:45:52
【问题描述】:
我正在尝试从我的计算机中读取文件并将其内容输出到文字控件中。它给出了错误:Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection. 这是我第一次使用 FileStream,我不是 100% 了解 VB 的所有语法,或者是否有社区首选的文件读取方式,但有人可以帮助我出了这个错误?
这是代码:
Using fs As New FileStream(_path, FileMode.Open, FileAccess.Read)
Try
Dim fileLength As Integer = CInt(fs.Length)
Dim buffer() As Byte = New Byte() {fileLength}
Dim count As Integer
Dim sum As Integer = 0
While ((count = fs.Read(buffer, sum, fileLength - sum)) > 0)
sum = sum + count
End While
litOutput.Text = buffer.ToString()
Catch ex As Exception
'TODO: log error
End Try
End Using
【问题讨论】:
-
File.ReadAllText怎么样?还有一个版本需要编码。
标签: vb.net filestream