How to delete a particular line from a file?

how to delete first line in a txt file

  • i am new in c#. I want to delete the first line in the txt file .

  • Answer:

    Basically you need to copy it to another file - you can't remove (or insert) data in a file "in place". For example, in .NET 4 it's easy: var lines = File.ReadLines("input.txt").Skip(1); File.WriteAllLines("output.txt", lines); It's slightly more work in older versions of .NET, at least if you want to avoid loading the whole file into memory, but still not too hard. Let me know if you need the code for an older version (and state which). EDIT: As discussed in comments, if you're willing to load the whole file into memory, you can write straight back to the same file: // Note that ReadAllLines is eager, not lazy like ReadLines var lines = File.ReadAllLines("file.txt").Skip(1); File.WriteAllLines("file.txt", lines);

Arshiya Gm at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.