Search code examples
c#text-filesread-writesystem.io.file

Open, remove text, and save a text file


I want to edit some text files automatically, but I don't know what to used to do it.

I want to open a file, check all lines, if one line begins with a 'C', I remove first 39-characters, etc .. then save all the file with an other name.

I already have a portion of code :

var car = ligne[0];
if(car != 'C') { continue; }
ligne.Remove(0, 39);

I use StreamReader to read, but what is the simple way to read and save in another file ?


Solution

  • Try File.ReadAllLines, which opens a file, reads everything, closes the file and returns an array containing all lines.

    Do your processing....

    Then File.WriteAllLines to open a file, write data, and close the file.