I need to add a header to a text file using this function:
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++;
}
For example: put the string "blah blah blah" to the top of the text file without overwriting the text that is there. I need the code to be implemented inside the function above
Keep in mind I only want the header written once. I am iterating through multiple text files using this function and appending to a new text file but only need the header written one time not everytime a text file is opened for parsing.