Adding a Line to the Middle of a File with .NET

后端 未结 6 1196
你的背包
你的背包 2020-12-03 21:45

Hello I am working on something, and I need to be able to be able to add text into a .txt file. Although I have this completed I have a small problem. I need to write the st

6条回答
  •  不知归路
    2020-12-03 22:28

    This is the best method to insert a text in middle of the textfile.

     string[] full_file = File.ReadAllLines("test.txt");
     List l = new List();
     l.AddRange(full_file);
     l.Insert(20, "Inserted String"); 
     File.WriteAllLines("test.txt", l.ToArray());
    

提交回复
热议问题