使用分隔符读取文本文件

时间:2015-03-09 11:06:08

标签: vb.net

我想阅读文本文件,并且从第五行开始只采用第一列(;)这个我的代码来浏览和阅读文本:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim OpenFile As New OpenFileDialog
        OpenFile.FileName = ""
        OpenFile.Filter = "Fichier Texte (*.pnp)|*.pnp"
        OpenFile.ShowDialog()
        Try
            Dim lire As New System.IO.StreamReader(OpenFile.FileName)
            RichTextBox1.Text = lire.ReadToEnd
            lire.Close()
        Catch ex As Exception

        End Try
    End Sub
End Class
请帮助我

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

    Dim lineYouWantToRead As Int32 = 5
    Dim fieldYouWantToRead As Int32 = 1
    Dim capturedValue As String = ""

    Using fileReader As New FileIO.TextFieldParser(OpenFile.FileName)
        fileReader.TextFieldType = FileIO.FieldType.Delimited
        fileReader.SetDelimiters(";")
        While fileReader.LineNumber <= lineYouWantToRead - 1
            Dim currentLine As String() = fileReader.ReadFields()
            capturedValue = currentLine(fieldYouWantToRead - 1)
        End While
    End Using

    RichTextBox1.Text = capturedValue

如果有任何帮助,请告诉我们。