how to overwrite data in a txt file? [duplicate]

2019-02-05 17:41发布

问题:

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());    
}

回答1:

You need to change the second parameter to false:

using (StreamWriter newTask = new StreamWriter("test.txt", false)){ 
        newTask.WriteLine(name[i].ToString());
}


回答2:

You're passing true as the append parameter.