Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Text In A Already Existing Text File VB.NET

I've been developing a arcade game, and as every good arcade game, it has an incorporated scoreboard so that players can see who scored better. My problem is that everytime it enters a new scoreline, it deletes all the previous lines in the text file. The code I've been using is the following:

    If player1 > 25 Then

        objReader.Close()
        MsgBox("O " + jogador1 + " ganhou.")
        tab1.Enabled = False

        Dim wrtScore As String = "C:\Documents and Settings\Joao\My Documents\Visual Studio 2010\Projects\flaghunter\flaghunter\deposito\scores.txt"
        Dim objWriter As New System.IO.StreamWriter(wrtScore)

        wrtScore = wrtScore.Trim()

        objWriter.WriteLine(jogador1 + " " + Str(player1))

        objWriter.Close()


    End If

Thank you for your attention and any help.

like image 548
joao Avatar asked Dec 04 '22 04:12

joao


2 Answers

Use File.AppendText instead:

    // This text is always added, making the file longer over time
    // if it is not deleted.
 Using sw As StreamWriter = File.AppendText(path)
        sw.WriteLine("This")
        sw.WriteLine("is Extra")
        sw.WriteLine("Text")
 End Using

This will create the file if it doesn't exist the first time and append the text if the file already exists

like image 70
Icarus Avatar answered Dec 31 '22 00:12

Icarus


Dim objWriter As New System.IO.StreamWriter(wrtScore, TRUE) to append to file :D

like image 29
Nguyễn Hoàng Gia Avatar answered Dec 31 '22 00:12

Nguyễn Hoàng Gia