且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在文本文件中的特定位置添加新行.

更新时间:2023-12-01 13:55:10

这将添加您想要的行.(确保你有 using System.IO;using System.Linq; 添加)

This will add the line where you want it. (Make sure you have using System.IO; and using System.Linq; added)

public void CreateEntry(string npcName) //npcName = "item1"
{
    var fileName = "test.txt";
    var endTag = String.Format("[/{0}]", npcName);
    var lineToAdd = "//Add a line here in between the specific boundaries";

    var txtLines = File.ReadAllLines(fileName).ToList();   //Fill a list with the lines from the txt file.
    txtLines.Insert(txtLines.IndexOf(endTag), lineToAdd);  //Insert the line you want to add last under the tag 'item1'.
    File.WriteAllLines(fileName, txtLines);                //Add the lines including the new one.
}