Reputation: 13
After appending a text file I want the console to write the new complete text of the txt file. However, the line I've just added is not written in the console. What am I doing wrong?
You can see my code below.
using System;
using System.IO;
namespace FileExercise
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\Text.txt";
//Read all lines
string lines = File.ReadAllText(path);
Console.WriteLine(lines);
//Add line to original document
File.AppendAllLines(@path, new string[] { "" + "This line is added
by Visual Studio" });
//Read new lines
Console.WriteLine(lines);
Console.ReadKey();
}
}
}
In the end, I expect to read the text already present in the file and the line "This line is added by Visual Studio". But all I'm getting is the old text.
Upvotes: 0
Views: 1118
Reputation: 2428
When you're appending to the file with File.AppendAllLines
the file is updated with the additional text but the values in lines
doesn't change, it still has the text that was in the file before appending.
There's a few ways to do what you want, here's one example:
//Read all the text in the file
string lines = File.ReadAllText(path);
//Output the original text
Console.Write(lines);
//Add your new line
lines += "This line is added by Visual Studio" + Environment.NewLine;
//Write the now updated text to the file
File.WriteAllText(path, lines);
//Output the new text
Console.Write(lines);
If you're planning to append many lines you may want to look into streaming the file and appending lines that way or even using the StringBuilder
.
Edit
I should have mentioned why I don't just call File.ReadAllText
again. This strategy would work but it's very inefficient as it would require that the entire contents of the file be read again. Depending on the size of the file this could take a significant amount of time. It's cleaner and more efficient to modify the data in memory like in the example above.
Upvotes: 1
Reputation: 1536
You should set the lines
variable again after appending your text, like you originally did.
lines = File.ReadAllText(path);
Which results in the following for you:
using System;
using System.IO;
namespace FileExercise
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\Text.txt";
//Read all lines
string lines = File.ReadAllText(path);
Console.WriteLine(lines);
//Add line to original document
File.AppendAllLines(@path, new string[] { "" + "This line is added
by Visual Studio" });
lines = File.ReadAllText(path);
//Read new lines
Console.WriteLine(lines);
Console.ReadKey();
}
}
}
Upvotes: 2
Reputation: 46
Afther appending the text to the file you need to read the file again. The 'Lines' variable still hold the text from the last time it reads the file.
string lines;
lines = File.ReadAllText(path);
Console.WriteLine(lines);
//Add line to original document
File.AppendAllLines(@path, new string[] { "" + "This line is added
by Visual Studio" });
//Read new lines
lines = File.ReadAllText(path);
Console.WriteLine(lines);
Upvotes: 0