The easiest way is to rewrite the entire file without the line (s) containing the word. You can use LINQ
to do this:
var oldLines = System.IO.File.ReadAllLines(path); var newLines = oldLines.Where(line => !line.Contains(wordToDelete)); System.IO.File.WriteAllLines(path, newLines);
If you want to delete all lines containing word (and not just a sequence of characters), you need to split the line into ' '
:
var newLines = oldLines.Select(line => new { Line = line, Words = line.Split(' ') }) .Where(lineInfo => !lineInfo.Words.Contains(wordToDelete)) .Select(lineInfo => lineInfo.Line);
source share