142

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!

Elfoc
  • 3,649
  • 15
  • 46
  • 57

5 Answers5

298

You could do it easily using

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

If you need newline

File.AppendAllText("date.txt", 
                   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.

Marco
  • 56,740
  • 14
  • 129
  • 152
  • 12
    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. – kͩeͣmͮpͥ ͩ Nov 24 '11 at 10:34
25

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));
Asken
  • 7,679
  • 10
  • 45
  • 77
  • 13
    Please use `Environment.Newline` and not `"\r\n"` - not every system agrees on how newlines work: https://en.wikipedia.org/wiki/Newline#Representations – kͩeͣmͮpͥ ͩ Nov 24 '11 at 10:32
13

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.

kad81
  • 10,712
  • 3
  • 38
  • 44
3
var Line = textBox1.Text + "," + textBox2.Text;

File.AppendAllText(@"C:\Documents\m2.txt", Line + Environment.NewLine);
Mehdi
  • 31
  • 4
1

Try the following code to create and write content to a text file in Unity3D.

void CreateLog() {
        string timestamp = DateTime.Now.ToString("dd-mm-yyyy_hh-mm-ss");
        PlayerPrefs.SetString("timestamp", timestamp);
        string path = Application.persistentDataPath + "/" + "log_" + timestamp + ".txt";
        // This text is added only once to the file.
        if (!File.Exists(path)) {
            // Create a file to write to.
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine(DateTime.Now.ToString() + ": " + "App initialised");
            }   
        } else {
            // This text is always added, making the file longer over time
            // if it is not deleted.
            using (StreamWriter sw = File.AppendText(path)) {
                sw.WriteLine(DateTime.Now.ToString() + ": " + "App initialised");
            }   
        }

        // Open the file to read from.
        using (StreamReader sr = File.OpenText(path)) {
            string line = "";
            while ((line = sr.ReadLine()) != null)
            {
                Debug.Log(line);
            }
        }
}
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81