I need to write a strings into a text file from C#, each string on a new line...How can I do this?
+7
A:
You can use File.WriteAllLines:
string[] mystrings = new string[] { "Foo", "Bar", "Baz", "Qux" };
System.IO.File.WriteAllLines("myfile.txt", mystrings);
dtb
2009-12-17 05:01:23
+1; can't beat a one-liner! Exception handling not included. Available in .NET 2.0+. http://msdn.microsoft.com/en-us/library/system.io.file.writealllines.aspx
p.campbell
2009-12-17 05:03:55
+1 This is one of the more simple ways to do it, but the least flexible.
Charlie Salts
2009-12-17 05:04:56
+2
A:
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);
SwDevMan81
2009-12-17 05:53:30