خواندن و نوشتن فایلهای متنی
کد:
using System.IO;
private void CreateTextFile()
{
using (FileStream fs = new FileStream("C:\\test.txt", FileMode.Create))
{
using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
{
w.WriteLine(124.23M);
w.WriteLine("Salaam!");
w.WriteLine('!');
}
}
}
private string ReadFromTextFile()
{
StringBuilder sb = new StringBuilder();
using (FileStream fs = new FileStream("C:\\test.txt", FileMode.Open))
{
using (StreamReader r = new StreamReader(fs, Encoding.UTF8))
{
sb.AppendLine(r.ReadLine());
sb.AppendLine(r.ReadLine());
sb.AppendLine(r.ReadLine());
}
}
return sb.ToString();
}