[C#] Read/Write from Text File



//first we need to import library
using System.IO;

Read:
//Read text Line-by-Line:
StreamReader reader = new StreamReader("textfile.txt");
  while (!reader.EndOfStream)
  {
     //using reader.ReadLine() for each line. Example:
  }
or
//Read all Text:
StreamReader reader = new StreamReader("textfile.txt");
//using reader.ReadToEnd() to get all the text from file. Example:
string text = reader.ReadToEnd();

Write:
//WriteAllLines auto create and write all the lines into a text file
string[] lines = { "First line", "Second line", "Third line" };
System.IO.File.WriteAllLines("textfile.txt", lines);
or
//Another Example with console:
While (true) //Make a loop
{
Console.Write("Write to file: ");
string line = Console.ReadLine();
if (line != "")
  writer.WriteLine(line);
else
  break; //if the string = nothing, stop the loop.
}