How to add new line into txt file

后端 未结 4 2006
执笔经年
执笔经年 2020-11-28 23:47

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 =         


        
相关标签:
4条回答
  • 2020-11-29 00:24

    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));
    
    0 讨论(0)
  • 2020-11-29 00:31

    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.

    0 讨论(0)
  • 2020-11-29 00:35
    var Line = textBox1.Text + "," + textBox2.Text;
    
    File.AppendAllText(@"C:\Documents\m2.txt", Line + Environment.NewLine);
    
    0 讨论(0)
  • 2020-11-29 00:48

    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.

    0 讨论(0)
提交回复
热议问题