An example from MSDN
http://msdn.microsoft.com/library/vstudio/8bh11f1k.aspx[
^]:
These examples show various ways to write text to a file. The first two examples use static methods on the System.IO.File class to write either a complete array of strings or a complete string to a text file. Example #3 shows how to add text to a file when you have to process each line individually before writing to the file. Examples 1-3 all overwrite all existing content in the file. Example #4 shows how to append text to an existing file.
class WriteTextFile
{
static void Main()
{
string[] lines = {"First line", "Second line", "Third line"};
System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
string text = "A class is the most powerful data type in C#. Like structures, " +
"a class defines the data and behavior of the data type. ";
System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
{
foreach (string line in lines)
{
if (line.Contains("Second") == false)
{
file.WriteLine(line);
}
}
}
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))
{
file.WriteLine("Fourth line");
}
}
}