File Crash?

HiTmAN

New member
Joined
Aug 10, 2003
Messages
1
I am using the following code:

Code:
Dim rdBfr As New System.IO.StreamReader(dlgO.FileName)
                Do While rdBfr.Read()
                    txtOriginal.Text = txtOriginal.Text & rdBfr.ReadLine()
                Loop
                rdBfr.Close()

However, the aplication crashes when I use it. What is the problem?
 
Try changing the Read() to Peek().

Anyway, you can read the whole thing like this:
Code:
txtOriginal.Text &= rdBfr.ReadToEnd()
 
and you may not need to use Do While , rather you can use While Not rdBfr.Peak() , heres a quick example :
Code:
        Dim dlgO As New OpenFileDialog()
        With dlgO
            .Filter = "text|*.txt"
            .InitialDirectory = "D:\"
            If .ShowDialog = DialogResult.OK Then
                Dim rdBfr As New IO.StreamReader(New IO.FileStream(dlgO.FileName, IO.FileMode.Open))
                While Not rdBfr.Peek /// make sure it only reads till it gets to its peak.
                    txtOriginal.AppendText(rdBfr.ReadLine & Environment.NewLine)
                End While
            End If
        End With
 
Back
Top