问题描述
我想用文字添加新行到我的date.txt文件,但不是将它添加到现有的date.txt,应用正在创造新的date.txt文件。
i'd like to add new line with text to my date.txt file, but instead of adding it into existing date.txt, app is creating new date.txt file..
TextWriter tw = new StreamWriter("date.txt");
// write a line of text to the file
tw.WriteLine(DateTime.Now);
// close the stream
tw.Close();
我想打开txt文件,添加一些文字,关闭它,后来点击后的东西:打开date.txt,添加文本,然后再合
I'd like to open txt file, add some text, close it, and later after clicking something: open date.txt, add text, and close it again.
这样我就可以:
按钮pressed:TXT打开 - >添加当前时间,然后将其关闭。 pssed另一个按钮$ P $,TXT打开 - >添加文本OK,或在同一行NOT OK,然后再次关闭它。
Button pressed: txt opened -> added current time, then close it. Another button pressed, txt opened -> added text "OK", or "NOT OK" in the same line, then close it again.
所以我的txt文件将looke这样的:
So my txt file will looke like that:
2011-11-24 10:00:00 OK
2011-11-25 11:00:00 NOT OK
我怎样才能做到这一点? TNX!
How can i do this? tnx!
推荐答案
您可以很容易地做到这一点使用
You could do it easily using
File.AppendAllText("date.txt", DateTime.Now.ToString());
如果你需要换行
File.AppendAllText("date.txt",
DateTime.Now.ToString() + Environment.NewLine);
无论如何,如果你需要你的code做到这一点:
Anyway if you need your code do this:
TextWriter tw = new StreamWriter("date.txt", true);
与第二个参数告诉追加到文件。结果
请在这里 StreamWriter的语法。
with second parameter telling to append to file.
Check here StreamWriter syntax.
这篇关于如何添加新的行成txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!