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();
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.
So i can get:
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.
So my txt file will look like that:
2011-11-24 10:00:00 OK
2011-11-25 11:00:00 NOT OK
How can i do this? Thanks!
This question is related to
c#
winforms
stream
text-files
Why not do it with one method call:
File.AppendAllLines("file.txt", new[] { DateTime.Now.ToString() });
which will do the newline for you, and allow you to insert multiple lines at once if you want.
No new line:
File.AppendAllText("file.txt", DateTime.Now.ToString());
and then to get a new line after OK:
File.AppendAllText("file.txt", string.Format("{0}{1}", "OK", Environment.NewLine));
var Line = textBox1.Text + "," + textBox2.Text;
File.AppendAllText(@"C:\Documents\m2.txt", Line + Environment.NewLine);
Source: Stackoverflow.com