Sometimes, we need to remove a specific line from a text file. For instance, when working with .csv files we often need to delete the 1st record as it is the header of csv file and is not required. This post describes how to delete a specific line from your file. The below code reads myFile.txt file and removes the 1st line from the file.
Source Code
List linesList = File.ReadAllLines("myFile.txt").ToList();
linesList.RemoveAt(0);
File.WriteAllLines("myFile.txt"), linesList.ToArray());
If you want to delete any specific line, modify Line 2 above and set the line number you want to delete and you're done.

LAVI RAJPUTPosted Nov 9, 2022, 11:57 AM
This code convert the user input pls help
Georgi MetodievPosted Sep 6, 2020, 2:02 PM
I had a university task to remove a line from a file which was presented in a ListBox. I used your code with the index of the selected item in the box. Worked fine. Thank you!
Nitesh KejriwalPosted Nov 1, 2012, 4:23 PM
Bicca, I agree this approach may be memory extensive on huge files, but I found it to be very fast. I tested with a file having around a million lines and it was fast!
BiccaPosted Nov 1, 2012, 3:13 PM
IMHO, Loading in memory the whole file could be risk if you are not sure how big the file can be. In that case I would go for a line by line approach.