H
H
Hydra2018-04-08 12:19:10
C++ / C#
Hydra, 2018-04-08 12:19:10

Working with files, how to remove a line from a txt file?

private void buttonDeleteCar_Click(object sender, EventArgs e)
        {
            string[] database = File.ReadAllLines("database.txt");
            int len = database.Count();
            string deletename = textBoxDeleteName.Text;

            for (int i = 0; i < len; i++) 
            {
                string line = database[i];
                string[] linemas = line.Split('|');
                string name = linemas[1];
                if (name == deletename) database[i] = ""; // Сравнение введенного текста с названием авто
            }
            StreamWriter del = new StreamWriter("database.txt", true);
            for (int i = 0; i < database.Count(); i++)
            {
                del.WriteLine(database[i]);
            }
        }

Nothing happens to the text file, what could be wrong?
An example from it:
1|Nissan GTR|10000|img\sportcars\car1.jpg
2|Lamborghini Aventador|15000|img\sportcars\car2.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2018-04-08
@Griboks

File.ReadAllLines
It is logical to use
File.WriteAllLines

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question