<分区>
我需要使用此函数向文本文件添加标题:
private static void WriteFile(string fileToRead, string fileToWrite, string mySearchString)
{
using (var sr = new StreamReader(fileToRead))
using (var sw = new StreamWriter(fileToWrite, true))
{
var count = 1;
while (sr.Peek() != -1)
{
var line = sr.ReadLine();
/* if (count == 3)
{
sw.WriteLine(line);
} */
if (count > 4)
{
if (line != null && line.Contains(mySearchString))
{
sw.WriteLine(line);
}
}
count++;
}
例如:将字符串“blah blah blah”放在文本文件的顶部而不覆盖那里的文本。我需要在上面的函数内部实现代码
请记住,我只想将标题写入一次。我正在使用此函数遍历多个文本文件并附加到一个新的文本文件,但只需要写一次标题,而不是每次打开文本文件进行解析时。