Pieter Brinkman

C#: Remove line from textfile

1 min readpieterASP.NetC#linqC#textfile

With the following code you can remove a line from a textfile (web.config). If the string is within a line the line will be removed.

string configFile = @"C:devweb.config";
List lineList = File.ReadAllLines(configFile).ToList();
lineList = lineList.Where(x => x.IndexOf("<!--") <= 0).ToList();
File.WriteAllLines(configFile, lineList.ToArray());