20/09/2012 by Nitesh

How to Remove a line from a text file at specific location

Quick Post –

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.

List linesList = File.ReadAllLines("myFile.txt").ToList();            
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.

Hope this helps!