0

This is the code i have so far, When a user gets a new high score it needs to clear the txt file and put the new high score in it or replace the number within the txt file. I am struggling to find a way to clear the file.

    ElseIf HighscoreDifficulty = "E" Then
        EasyHighScore = My.Computer.FileSystem.ReadAllText("EasyHighScore.txt")
        If CurrentScore > EasyHighScore Then
            NewHighScore.Visible = True
            file = My.Computer.FileSystem.OpenTextFileWriter("EasyHighScore.txt", True)
            file.WriteLine(CurrentScore)
            file.Close()
        Else
            NoNewHighScore.Visible = True
        End If

Thanks

Nightz
  • 5
  • 1
  • See [this](https://stackoverflow.com/questions/5030250/how-do-i-overwrite-text-in-vb-net) similar question – Anu6is Apr 26 '20 at 11:55

1 Answers1

0

Let's say that you want to keep the top five scores in the file. Assuming that the file always contains valid data, you could do that like this:

Private Sub SaveHighScore(score As Integer)
    Const FILE_PATH = "file path here"
    Const MAX_SCORE_COUNT = 5

    'Read the lines of the file into a list of Integer values.
    Dim scores = File.ReadLines(FILE_PATH).
                      Select(Function(s) CInt(s)).
                      ToList()

    'Append the new score.
    scores.Add(score)

    'Sort the list in descending order.
    scores.Sort(Function(x, y) y.CompareTo(x))

    'Write up to the first five scores back tot he file.
    File.WriteAllLines(FILE_PATH,
                       scores.Take(MAX_SCORE_COUNT).
                              Select(Function(i) i.ToString()))
End Sub

By adding the new score to the existing list, sorting and then writing out the first five, you automatically drop the lowest score. That means that there's never a need to actually check whether the new score is a high score or not.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • I have a seperate high scores screen that displays the highest scores in easy, medium and hard, Is there no way to clear or replace a text files content with nothing or spaces to then be able to add a value to it? – Nightz Apr 26 '20 at 12:11
  • 1
    Of course, but why would you? Just write what you want to write to the file and what was there before will be overwritten. You don't have to explicitly clear the file. Just call `WriteAllLines` as I have and pass the lines you want to write. That will overwrite whatever was written before. – jmcilhinney Apr 26 '20 at 12:18
  • That said, what you have already doesn't mean that you can't use what I provided. You'd just break it up a little. You can still read the high scores as I have shown. You can display them to the user in whatever way is appropriate. You can then incorporate the new score into the high scores just as I have shown and display to the user again and/or save. Don't disregard good logic just because you don't want to use it all at the same time. – jmcilhinney Apr 26 '20 at 12:20
  • Sorry that I am really dumb lol, I have got this file.WriteAllLines("EasyHighScore.Txt", CurrentScore) But it says that writealllines is not a member of streamwriter EDIT: Fixed it :D did: File.WriteAllText("EasyHighScore.Txt", CurrentScore) Thanks for all the help and sorry im so dumb – Nightz Apr 26 '20 at 12:32