C#: Remove line from textfile

by Pieter Brinkman 26. January 2010 03:29

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.

[code:c#]

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

[/code]

 

Tags: , ,
Categories: ASP.Net | Linq | C#