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 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
Naimish MakwanaPosted Feb 16, 2023, 7:21 AM
Hello Haga,
Try to use List.Insert instead of
List.Addmethod.Example:
Thanks
Naimish Makwana
Haga AndrianavalonaPosted Feb 16, 2023, 7:32 AM
thanks Naimish Makwana, It worked perfectly.