I got a text file with e.g. 3 lines:
Example Text
Some text here
Text
I want to add some text directly after "here", so it will look like this:
Example Text
Some text hereADDED TEXT
Text
My code, so far, looks like this, I used some of the code from here, but it doesn't seem to work.
List<string> txtLines = new List<string>();
string FilePath = @"C:\test.txt";
foreach (string s in File.ReadAllLines(FilePath))
{
txtLines.Add(s);
}
txtLines.Insert(txtLines.IndexOf("here"), "ADDED TEXT");
using (File.Create(FilePath) { }
foreach (string str in txtLines)
{
File.AppendAllText(FilePath, str + Environment.NewLine);
}
My problem is:
txtLines.IndexOf("here")
returns -1
, thus throwing a System.ArgumentOutOfRangeException
.
Can somebody tell me what I am doing wrong?