How to write text to a specific line in Visual Studio C#

I'm wondering if there is a way to write data from the program to a specific line on an external text document.

For example, so far within my program I have a string array which gets the users input and saves it in the corresponding element and into the text file. It can read the first element fine, however how do I get it so when I enter the input for element 2 it overwrites what is on line 2 so when it reads the text file it displays the newest input into a label?

Jon Skeet
people
quotationmark

Well, the simplest approach would be:

string[] lines = File.ReadAllLines(path);
lines[index] = newText;
File.WriteAllLines(path, lines);

However, that assumes that there's already an appropriate number of lines in the file - is that always the case?

Note that this is also potentially inefficient in memory if the file is very large - but making it more efficient would also make the code more complicated.

people

See more on this question at Stackoverflow