I am trying to read a file from my computer and output its contents into a literal control. It gives the error : 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. This is my first time using FileStream and I'm not 100% about all of the syntax for VB, or if there's even a community-preferred way of reading from a file, but could somebody help me out with this error?
This is the code:
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? There is also a version that takes an encoding.