开发者

How to write a text file in C#

开发者 https://www.devze.com 2022-12-14 01:31 出处:网络
I need to write a strings into a text file from C#, each string on 开发者_StackOverflowa new line...How can I do this?You can use File.WriteAllLines:

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


You can use File.WriteAllLines:

string[] mystrings = new string[] { "Foo", "Bar", "Baz", "Qux" };

System.IO.File.WriteAllLines("myfile.txt", mystrings);


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


How about StreamWriter class? Read more here...

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


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

精彩评论

暂无评论...
验证码 换一张
取 消