在VB.net中的多行.txt文件中使用TextFieldParser

时间:2014-04-25 04:10:22

标签: vb.net visual-studio-2010 file-io

我正在为VB Programming类开发一个项目,我正在尝试创建一个播放器注册和配置文件程序。我们不允许使用数据库,因为我们的教授希望我们为此项目使用.txt文件。

我已经想出如何将信息写入.txt文件并解析出.txt文件的第一行以显示正确字段中的信息,但我无法弄清楚如何解析多行。我需要读取.txt文件并查找特定行并使用该行中的所有信息填写表单字段。

以下是我必须解析第一行的内容。

 Imports Microsoft.VisualBasic.FileIO
 Imports System.IO

Public Class Profile

Private PlayerInfoParser As TextFieldParser
Friend index As Integer = Home.cbPlayerLookUp.SelectedIndex

Private Sub Profile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim FileString As String = "MasterRoster.txt"

    Try
        PlayerInfoParser = New TextFieldParser(FileString)
        PlayerInfoParser.TextFieldType = FieldType.Delimited
        PlayerInfoParser.SetDelimiters(",")
    Catch
        MessageBox.Show("Error")
    End Try


    Dim FieldString() As String

    If Not PlayerInfoParser.EndOfData Then
        FieldString = PlayerInfoParser.ReadFields()
        txtFirstNameProfile.Text = FieldString(0)
        txtLastNameProfile.Text = FieldString(1)
    Else

    End If

目前,这只会显示.txt文件的第一行(“MaserRoster.txt”)。我无法弄清楚如何检查第二行。

感谢您的任何建议。

2 个答案:

答案 0 :(得分:1)

发生的事情是你没有遍历你的行,你只执行一次ReadField,将它括在While语句而不是If语句中,然后你可以迭代你的所有行

While Not PlayerInfoParser.EndOfData
    FieldString = PlayerInfoParser.ReadFields()
    txtFirstNameProfile.Text = FieldString(0)
    txtLastNameProfile.Text = FieldString(1)
End While

正如我在评论中提到的,我可能只是使用VB.net中的File对象。它有一个ReadLines方法,可以一次读取所有行,然后你可以迭代它们并解析它们。我创建了一个自定义类,它将保存您从文件中读取的信息,然后创建该类的列表。然后,您可以选择所需的记录并显示它。

Imports System.IO
Public Class Form1
    Dim playerList As List(Of player) = New List(Of player)
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        txtFirstNameProfile.Text = playerList(1).firstname
        txtLastNameProfile.Text = playerList(1).lastname

    End Sub
    Private Sub Profile_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim FileString As String = "MasterRoster.txt"

        Dim values() As String = File.ReadAllLines(FileString)
        Dim FieldString() As String
        If Not values.Length = 0 Then
            For Each s As String In values
                FieldString = s.Split(","c)
                playerList.Add(New player(FieldString(0), FieldString(1)))
            Next
        End If

    End Sub
End Class
Class player
    Dim mFirst As String
    Dim mLast As String
    Public ReadOnly Property firstname
        Get
            Return mFirst
        End Get
    End Property
    Public ReadOnly Property lastname
        Get
            Return mLast
        End Get
    End Property
    Public Sub New(first As String, last As String)
        mFirst = first
        mLast = last
    End Sub
    Public Overrides Function ToString() As String
        Return mFirst & "," & mLast
    End Function
End Class

或者,如果你只想抓住一条这样的东西应该有效。

Dim values() As String = File.ReadAllLines(FileString)
Dim FieldString() As String
If Not values.Length < index Then  'The index is zero based
    FieldString = values(index).Split(","c)
    txtFirstNameProfile.Text = FieldString(0)
    txtLastNameProfile.Text = FieldString(1)
End If

最后,如果你想继续使用TextFieldParser,你可以使用这样的东西。

While Not PlayerInfoParser.EndOfData
    FieldString = PlayerInfoParser.ReadFields()
    PlayerList.Add(New player(FieldString(0), FieldString(1)))
End While

答案 1 :(得分:0)

这是一个非常简单的练习,使用虚构的文本文件读取每一行,这是文本文件的内容:

player1, score: 0.385, games: 5
player2, score: 0.345, games: 3
player3, score: 0.235, games: 7
player4, score: 0.319, games: 6
player5, score: 0.296, games: 9

这是一次读取每一行的代码:

Module Module1
Sub Main()

    Dim file As New IO.StreamReader("C:\Users\fernando\Desktop\players.txt")
    Dim line As String

    While file.Peek <> -1
        line = file.ReadLine()
        Console.WriteLine(line)
    End While

    Console.Read()
End Sub
End Module