简体   繁体   中英

Read content from file using StreamReader (vb.net)

I have a txt file containing

line1
line2
line3

I am trying to read the content of that file but i am getting this instead

line1 line2 line3

Basically I want to print the new lines too.

This is what I have tried so far. The user uploads a file, clicks a button and gets the content of the file. Here is my method. Could someone point me what's wrong?

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
    Dim path As String = FileUpload1.PostedFile.FileName
    Dim list As New List(Of String)
    If (FileUpload1.HasFile) Then
        Using r As StreamReader = New StreamReader(path)
            Dim line As String
            line = r.ReadLine()
            Do While (Not line Is Nothing)
                list.Add(line)
                Response.Write(line)
                Response.Write(Environment.NewLine)
                line = r.ReadLine()
            Loop
        End Using

    End If
End Sub

Try to write the same thing directly in HTML and see what happen. You will notice that HTML doesn't care about multiple spaces and new lines. You'll have to use proper tag:

<p> <br> <pre>

Or css

white-space, word-wrap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM