By Elfoc

2011-11-24 10:26:12 8 Comments

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

// close the stream

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!


@Mehdi 2017-05-28 07:18:17

var Line = textBox1.Text + "," + textBox2.Text;

File.AppendAllText(@"C:\Documents\m2.txt", Line + Environment.NewLine);

@kad81 2016-12-20 23:50:39

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.

@twoleggedhorse 2018-07-13 14:54:33

I prefer this to the accepted answer; you don't need to specify new line

@Asken 2011-11-24 10:30:35

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

@David Kemp 2011-11-24 10:32:18

Please use Environment.Newline and not "\r\n" - not every system agrees on how newlines work:

@Marco 2011-11-24 10:27:19

You could do it easily using

File.AppendAllText("date.txt", DateTime.Now.ToString());

If you need newline

                   DateTime.Now.ToString() + Environment.NewLine);

Anyway if you need your code do this:

TextWriter tw = new StreamWriter("date.txt", true);

with second parameter telling to append to file.
Check here StreamWriter syntax.

@David Kemp 2011-11-24 10:34:16

If you're using c# 4 (or newer) compiler, you can put new StreamWriter("date.txt", append:true) to make the intention a little clearer.

Related Questions

Sponsored Content

60 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

47 Answered Questions

[SOLVED] How do I update the GUI from another thread?

27 Answered Questions

[SOLVED] How do I enumerate an enum in C#?

7 Answered Questions

[SOLVED] How can I redirect and append both stdout and stderr to a file with Bash?

37 Answered Questions

[SOLVED] How to get line count cheaply in Python?

18 Answered Questions

[SOLVED] Why should text files end with a newline?

15 Answered Questions

[SOLVED] How can I update the current line in a C# Windows Console App?

  • 2009-05-20 15:12:53
  • IVR Avenger
  • 179795 View
  • 454 Score
  • 15 Answer
  • Tags:   c# windows console

9 Answered Questions

[SOLVED] How do I save a stream to a file in C#?

  • 2009-01-04 20:04:39
  • Loadman
  • 673730 View
  • 646 Score
  • 9 Answer
  • Tags:   c# .net stream

12 Answered Questions

[SOLVED] Create a .txt file if doesn't exist, and if it does append a new line

  • 2012-03-28 12:39:58
  • Berker Yüceer
  • 371108 View
  • 139 Score
  • 12 Answer
  • Tags:   c# text-files

2 Answered Questions

[SOLVED] How to add a new line into a txt file using PHP

  • 2018-01-27 12:04:04
  • James69
  • 39 View
  • 0 Score
  • 2 Answer
  • Tags:   php text-files

Sponsored Content