c# save string 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: asp.net c# write string to text file
using (StreamWriter writer = new StreamWriter("email.txt", true))
{
writer.WriteLine("your_data");
}
Example 3: 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);