C# Read File

Open and read a file into streams:

using System.IO;
StreamReader sr = new StreamReader("test.txt");
string strLine = sr.ReadLine();
while (!sr.EndOfStream)
{
...
strLine = sr.ReadLine();
}
sr.Close();

If the file is relatively small, it can be read as one string at one time.
using System.IO.File;
string str = File.ReadAllText("Text.txt");

To read a file line by line.
using System.IO.File;
string[] arrlines = File.ReadAllLines("Text.txt");
foreach (string thisline in arrlines)
{
...
}


endmemo.com © 2017  Terms of Use | Home