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

瘦欲@ 提交于 2019-11-29 10:58:04

问题


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.



来源:https://stackoverflow.com/questions/6445125/how-to-overwrite-data-in-a-txt-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!