从文本文件中读取并填充文本框

时间:2012-05-08 14:40:31

标签: vb.net visual-studio-2010 readfile

好的,这是我目前的代码。我无法读取文件并相应地填充文本框。加载文件后(我认为是)在下面列出的任何文本框中都没有填充任何内容。基本上我不确定文件是否被读取。

有什么建议吗?

        If OpenFileDialog2.ShowDialog() = DialogResult.OK Then
        If My.Computer.FileSystem.FileExists("path_to_file") Then
            Dim ioFile As New System.IO.StreamReader("path_to_file" + "filename")
            TextBox1.Text = ioFile.ReadLine() 'Adds the first line
            TextBox2.Text = ioFile.ReadLine() 'Adds the second line
            TextBox3.Text = ioFile.ReadLine() 'Adds the third line
            TextBox4.Text = ioFile.ReadLine() 'Adds the fourth line
            TextBox5.Text = ioFile.ReadLine() 'Adds the fifth line
        End If
    End If

1 个答案:

答案 0 :(得分:1)

呃,这是什么

If My.Computer.FileSystem.FileExists("path_to_file") Then
  Dim ioFile As New System.IO.StreamReader("path_to_file" + "filename")

应该是

If My.Computer.FileSystem.FileExists(OpenFileDialog2.FileName) Then
  Dim ioFile As New System.IO.StreamReader(OpenFileDialog2.FileName)