vb.net 从 .txt 文件中读取并显示内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25529873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:07:17  来源:igfitidea点击:

vb.net Reading from a .txt file and displaying the contents

vb.netfilestreamreader

提问by Novastorm

I'm making a simple program which reads and writes .txt files. I've got the program to write to and save a .txt file however I'm having some trouble reading from .txt files. Here's what I've got so far:

我正在制作一个简单的程序来读取和写入 .txt 文件。我有要写入并保存 .txt 文件的程序,但是我在读取 .txt 文件时遇到了一些麻烦。这是我到目前为止所得到的:

Using openTxt As New OpenFileDialog()
    If openTxt.ShowDialog() = Windows.Forms.DialogResult.OK Then
        Dim displayForm As New Form
        Dim textReader As New System.IO.StreamReader(openTxt.FileName)
        displayForm.ListBox1.Text = textReader.ReadToEnd
        textReader.Close()
        displayForm.Show()
    Else
        MessageBox.Show("Not a text file")
    End If
End Using

What I would like to happen is when the text has been read it populates in a list box which is present inside another form (displayForm). I've tried getting the text to display in a listbox on the same form to see if that might have changed anything but it still remains blank. I can confirm that I've only ever tested it with .txt files as I've put no error checking in at this stage. Many thanks for any help!

我想要发生的是,当文本被读取时,它会填充到另一个表单(displayForm)中的列表框中。我试过让文本显示在同一个表单的列表框中,看看是否有任何改变,但它仍然保持空白。我可以确认我只用 .txt 文件测试过它,因为我在这个阶段没有检查错误。非常感谢您的帮助!

回答by WeSt

A ListBoxis not for displaying text, but displaying lists (as the name suggests). If you want to display text, use a TextBox. Since it is likely that the file will contain multiple lines, you can set the .Multilineproperty to True, so that the TextBoxwill display it correctly.

AListBox不是用于显示文本,而是用于显示列表(顾名思义)。如果要显示文本,请使用TextBox. 由于文件很可能包含多行,您可以将该.Multiline属性设置为True,以便TextBox正确显示它。

Furthermore, you should use the using statementwhen dealing with Streams

此外,您应该using statement在处理时使用Streams

Dim content As String = ""
Using textReader As New System.IO.StreamReader(openTxt.FileName)
  content = textReader.ReadToEnd
End Using
displayForm.ListBox1.Text = content

or simply use the System.IO.File.ReadAllText("path to file here")command.

或者干脆使用System.IO.File.ReadAllText("path to file here")命令。

回答by Jeff Reed

Do you want to read the file line-by-line and populate the listbox control?

是否要逐行读取文件并填充列表框控件?

If that's the case then try this function

如果是这样,那么试试这个功能

Function ReadFile(ByVal Filename As String) As String()
    Dim Sl As New List(Of String)
    Using Sr As New StreamReader(Filename)
        While Sr.Peek >= 0
            Sl.Add(Sr.ReadLine())
        End While
    End Using
    Return Sl.ToArray
End Function

And use like so:

并像这样使用:

    For Each Line As String In ReadFile("FILENAME.txt")
        ListBox1.Items.Add(Line)
    Next