How to duplicate line and insert it above the selected line ?

Feb 16 2023 7:08 AM

I want to duplicate a line from a text file (.txt) and insert it above the line selected when a button is clicked

eg:

Tomato

Lemon

Apple

and I want to duplicate "Lemon" and insert it above when I click a button, like :

Tomato

Lemon

Lemon <- Inserted Line

Apple

I've tried :

void duplLine(string pathFile, int line)
{
     List<string> tempFile = File.RealAllLines(pathFile).ToList();
     tempFile.Add(tempFile[line]);
     File.writeAllLines(pathFile, tempFile.ToArray();
}

It worked perfectly but the line was inserted at the END like :

Tomato

Lemon

Apple

Lemon <- Inserted Line (This should be at the index 2)

Thanks


Answers (2)