Answer the question
In order to leave comments, you need to log in
How to remove lines from a text file?
For example, player 1 was in 5th place, and some next player 2 broke this record and became in 5th place, and player 1 in 6th place, you need to remove the player who is in 6th place from a file!
this code has a dictionary - key - player name, value - time in seconds (less is better)
All results (key and value) are written to a file in a different form, and in this form they are copied to the dictionary and scrolled from the dictionary by label as the end result, but I can't remove the players from the file that left the rating.
How to delete this player in a file?
private static Dictionary<string, int> AllNames()
{
return File
.ReadLines(@"C:\Users\HP\Desktop\картинки\results.txt")
.Where(line => !string.IsNullOrWhiteSpace(line))
.Select(item => item.Split(' '))
.ToDictionary(items => items[0],
items => int.Parse(items[1]));
}
private void updateRatingLabels()
{
var tops = AllNames()
.OrderBy(pair => pair.Value)
.ThenBy(pair => pair.Key, StringComparer.Ordinal)
.Take(5)
.ToArray();
for (int i = 18; i <= 22; ++i)
Controls.Find($"label{i}", true).First().Text = "";
for (int i = 28; i <= 32; ++i)
Controls.Find($"label{i}", true).First().Text = "";
for (int i = 0; i < tops.Length; ++i)
{
Controls.Find($"label{i + 18}", true).First().Text = tops[i].Key;
Controls.Find($"label{i + 28}", true).First().Text = $"{tops[i].Value / 60}:{tops[i].Value % 60:00}";
}
}
Answer the question
In order to leave comments, you need to log in
Read all lines from a file into memory. In memory, delete those lines that you need to delete.
Write back the list of strings to the file.
var lines = File.ReadAllLines("lines.txt").ToList();
lines.RemoveAt(indexToRemove);
File.WriteAllLines("lines.txt", lines);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question