简体   繁体   English

使用StreamReader(vb.net)从文件读取内容

[英]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. 尝试直接用HTML编写相同的内容,然后看看会发生什么。 You will notice that HTML doesn't care about multiple spaces and new lines. 您会注意到HTML并不关心多个空格和换行符。 You'll have to use proper tag: 您必须使用适当的标签:

<p> <br> <pre>

Or css 或CSS

white-space, word-wrap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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