You can make use of File.ReadLines
together with Enumerable.First
.
This gurantees 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 doesnt read the wole file into an array of lines first.
Edit :
Linq also makes sure of properly disposing the FileStream.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…