c# write collection to file code example
Example 1: c# create a text file
string fileName = @"C:\Temp\Temp.txt";
if (!File.Exists(fileName))
{
using (StreamWriter writer = File.CreateText(fileName))
{
writer.WriteLine("Hello World");
}
}
Example 2: List string to file C#
string[] lines = { "First line", "Second line", "Third line" };
System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
lines = { "Fourth line", "Fith line" };
System.IO.File.AppendAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);