Reputation: 111
I want to add a new line to my txt file. I tried to write a new line like this:
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\text.txt",true))
{
file.WriteLine("SOME TEXT");
}
In this way it goes to the last line and writes, but if I want to write, for example on the fourth line without deleting the old data, how I can do this? How I can define that this string must be written in the forth line?
Thank for atention.
Upvotes: 0
Views: 98
Reputation: 4002
You may use this:
var lines = File.ReadLines(@"C:\text.txt").ToList();
lines.Insert(4, "SOME TEXT");
File.WriteAllLines(@"C:\text.txt", lines);
Upvotes: 8