Remove particular line in a .txt file and replace with new line vb.net

418 Views Asked by At

I've seen this question on here a few times but I can't seem to find the right answer. I currently have a Windows Form Application I'm writing in VB that creates player profiles (this is a school project). On the 'Registration' page I take in the information and read it to a .txt file with each field comma deliminator line by line. This makes for a .txt that looks something like this:

 Dan,Boyle,M,39,(707) 555-1234,555 S. West Street,18,D,R
 Justin,Braun,M,27,(916) 555-1255,858 W. South St,5,D,L

I ran right new lines to this, and call this just fine but what I need to do for my project is be able to edit a player profile. What I've found so far is that the best way would be to re-write the whole line, which I've managed to do. But what I can't figure out is how to remove the current line and replace.

    Dim FileString As String = "MasterRoster.txt"
    Dim index As Integer = Home.cbPlayerLookUp.SelectedIndex

    Dim playerInfo() As String = File.ReadAllLines(FileString)
    Dim FieldString() As String

    Dim RecordString As String = ""


    If Not playerInfo.Length < index Then
        FieldString = playerInfo(index).Split(","c)
        'Update Player stats)

        If CInt(FieldString(16)) = index Then

            FieldString(12) = CStr(CInt(FieldString(12)) + CInt(txtGoals.Text))
            FieldString(13) = CStr(CInt(FieldString(13)) + CInt(txtAssists.Text))
            FieldString(14) = CStr(CInt(FieldString(14)) + CInt(txtPIM.Text))
            FieldString(15) = CStr(CInt(FieldString(15)) + CInt(txtGames.Text))

            '(FirstName0, LastName1, Gender2, Age3, Phone4, Address5, Experience6, Position7, Handedness8, Team9, Jersey#10, imgPath11, Goals12, Assists13, PIM14, Total Games15, playerID Number16)
            RecordString = FieldString(0) & "," & FieldString(1) & "," & FieldString(2) & "," & FieldString(3) & "," & FieldString(4) & "," & FieldString(5) &
                "," & FieldString(6) & "," & FieldString(7) & "," & FieldString(8) & "," & FieldString(9) & "," & FieldString(10) & "," & FieldString(11) & "," & FieldString(12) & "," &
                FieldString(13) & "," & FieldString(14) & "," & FieldString(15) & "," & FieldString(16) & vbNewLine



        End If

        My.Computer.FileSystem.WriteAllText(FileString, RecordString, True)

    End If

This will currently add a new line to the bottom of my .txt file "MasterRoster.txt" with the updated information at the end, but the old one is still there. How can I instead replace the old line?

1

There are 1 best solutions below

2
tinstaafl On

Since you are reading the whole file into memory, using a class to represent the info allows you to put meaningful names to each field.(Will you remember what each field represents a year or two from now?) One way to hold a collection of that data is in a DataTable which is very easy to use with a DataGridView, to display the data for reading and editing. Another option is to use an .xml text format instead of a .csv text format. This allows easier reading and writing to and from the file, since DataTable has methods designed for that.

In any case editing specific records will require over-writing or re-writing the file, unless, of course you want to take a step backwards and use the old legacy methods, FileOpen, FileGet, and FilePut.