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

后端 未结 6 1199
你的背包
你的背包 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:35

    Check out File.ReadAllLines(). Probably the easiest way.

         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());
    

提交回复
热议问题