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.
You may use this:
var lines = File.ReadLines(@"C:\text.txt").ToList();
lines.Insert(4, "SOME TEXT");
File.WriteAllLines(@"C:\text.txt", lines);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.