24

Possible Duplicate:
Can any one tell why the previous data is still displayed while saving data using StreamWriter

I have WPF C# application, that reads and writes to a .txt file, i know how to write line but line, but how do I overwrite the text that is already the file. This is what I have just to write to the next line of the text file, but I want to over the lines not just write to the next line, thanks.

using (StreamWriter newTask = new StreamWriter("test.txt", true)) 
{
    newTask.WriteLine(name[i].ToString());    
}
Community
  • 1
  • 1
Beef
  • 1,373
  • 5
  • 18
  • 36

2 Answers2

65

You need to change the second parameter to false:

using (StreamWriter newTask = new StreamWriter("test.txt", false)){ 
        newTask.WriteLine(name[i].ToString());
}
Ryan Gross
  • 6,083
  • 26
  • 43
  • 3
    What about when your goal is to replace ALL the contents of the file, and then new content contains less lines then the old. Will the entire file be overwritten, or will you have the remaining lines of the old file present? – Chris Feb 04 '16 at 21:56
  • 3
    It will replace the entire file. – Ryan Gross Mar 05 '16 at 02:20
11

You're passing true as the append parameter.

SLaks
  • 800,742
  • 167
  • 1,811
  • 1,896