I want to read a text file line by line and edit a specific line. So, I have put the text file into a string variable like:
string textFile = File.ReadAllText(filename);
My text file is like:
Line A
Line B
Line C
Line abc
Line 1
Line 2
Line 3
I have a specific string (="abc"), which I want to search in this textFile. So, I am reading the lines until find the string and going to the third line ("Line 3" -> this line is always different) after that found string:
string line = "";
string stringToSearch = "abc";
using (StringReader reader = new StringReader(textFile))
{
while ((line = reader.ReadLine()) != null)
{
if (line.Contains(stringToSearch))
{
line = reader.ReadLine();
line = reader.ReadLine();
line = reader.ReadLine();
//line should be cleared and put another string to this line.
}
}
}
I want to clear the third read line and put another string to this line and save the whole string
into textFile
.
How can I do this?