Skip to content

Instantly share code, notes, and snippets.

@musoftware
Last active November 21, 2016 21:03
Show Gist options
  • Select an option

  • Save musoftware/3fef908a4d265ac9bb1c33fac6b2cd9e to your computer and use it in GitHub Desktop.

Select an option

Save musoftware/3fef908a4d265ac9bb1c33fac6b2cd9e to your computer and use it in GitHub Desktop.
Read Lines from file into Memory,Directly From HDD
var fileRead = System.IO.File.OpenText("test.txt");
while (fileRead.Peek() != -1)
{
string line = fileRead.ReadLine();
// here your code
}
fileRead.Close();
public static IEnumerable<string> ReadLinesFile(string filename)
{
var fileRead = System.IO.File.OpenText(filename);
while (fileRead.Peek() != -1)
{
string line = fileRead.ReadLine();
yield return line;
}
fileRead.Close();
}
/* usage */
foreach (var line in ReadLinesFile("test.txt"))
{
// here your code
}
/******/
foreach (var item in System.IO.File.ReadLines("test.txt"))
{
}
string[] lines = System.IO.File.ReadAllLines("test.txt", Encoding.UTF8);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment