How to write a text file in C#

前端 未结 4 625
抹茶落季
抹茶落季 2020-12-07 02:48

I need to write a strings into a text file from C#, each string on a new line...How can I do this?

相关标签:
4条回答
  • 2020-12-07 03:27

    If you wish to append the text lines to the file, use AppendAllText:

    string appendText = "This is extra text" + Environment.NewLine;
    File.AppendAllText(path, appendText);
    
    0 讨论(0)
  • 2020-12-07 03:29

    How about StreamWriter class? Read more here...

    And do not forget about exception handling e.g missing file permissions etc.

    0 讨论(0)
  • 2020-12-07 03:33

    Use the StreamWriter class; take a look at this tutorial. A new line is simply the character \n (Unix) or the characters \r\n (Windows).

    0 讨论(0)
  • 2020-12-07 03:39

    You can use File.WriteAllLines:

    string[] mystrings = new string[] { "Foo", "Bar", "Baz", "Qux" };
    
    System.IO.File.WriteAllLines("myfile.txt", mystrings);
    
    0 讨论(0)
提交回复
热议问题