I've been trying to write new texts in a specific line without completely overwriting the current contents of the file, but I can't seem to make it work. For example, in my file called Test.txt
. This is what's currently written in it:
1
2
3
4
8
9
10
As you may have noticed, I did not include the numbers 5,6 and 7. There is also a space where you can insert 5,6 and 7. But as I tried to insert those numbers, it's giving me this output:
1
2
3
4
8
9
101
2
3
4
5
6
7
It clearly have added a bunch of numbers that I did not want. Code for this is indicated below. Hope you guys can help me on this one.
namespace TestCode
{
class Program2
{
static void Main()
{
var lines = System.IO.File.ReadAllLines(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\Test.txt");
lines[5] = "5";
lines[6] = "6";
lines[7] = "7";
System.IO.File.AppendAllLines(@"C:\Users\User1\Documents\Visual Studio 2015\WebSites\MusicStore\Pages\Test.txt", lines);
}
}
}