Write Text to a File : Text File Read Write « File Stream « C# / C Sharp
- C# / C Sharp
- File Stream
- Text File Read Write
Write Text to a File
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
StringBuilder sb = new StringBuilder();
foreach (string txtName in Directory.EnumerateFiles(mydocpath,"*.txt"))
{
using (StreamReader sr = new StreamReader(txtName))
{
sb.AppendLine(txtName.ToString());
sb.Append(sr.ReadToEnd());
sb.AppendLine();
}
}
using (StreamWriter outfile = new StreamWriter(mydocpath + @"\AllTxtFiles.txt"))
{
outfile.Write(sb.ToString());
}
}
}
Related examples in the same category