tags:

views:

201

answers:

4

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

A: 

See: StreamWriter

Qberticus
+7  A: 

You can use File.WriteAllLines:

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

System.IO.File.WriteAllLines("myfile.txt", mystrings);
dtb
+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
+1 This is one of the more simple ways to do it, but the least flexible.
Charlie Salts
A: 

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

ty
+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