Blog

How to Remove a Line From a Text File at Specific Location in C#

Posted by Nitesh Luharuka Blogs | C# Language Oct 30, 2012
In this blog, we will see how to remove a line from a text file at specific location in C#

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.

COMMENT USING
PREMIUM SPONSORS
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
Employers - Post Free Jobs
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
Employers - Post Free Jobs