Read only first line from a text file

You can make use of File.ReadLines together with Enumerable.First.
This guarantees you to only read the first line from the file.

using System.Linq; 

string line1 = File.ReadLines("MyFile.txt").First(); // gets the first line from file.

The difference to File.ReadAllLines is, that File.ReadLines makes use of lazy evaluation and doesn’t read the whole file into an array of lines first.

Linq also makes sure of properly disposing the FileStream.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)