Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

286
Views
Does File.ReadLines(filePath).First() close the file immediately?

I know that when using the IEnumerable returned by File.ReadLines() in a foreach loop, the file gets closed automatically after the loop. I just need to quickly check the first line of a file. Is this enough or will it keep the file open?

protected void Append(string filePath, Encoding encoding)
{
    try
    {
        string firstLine = File.ReadLines(filePath, encoding).First();
        // more code here
    }
    catch
    {
        // more code here
    }
}
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

(Note that this is for File.ReadLines() which returns an IEnumerable<String> - this is not for File.ReadAllLines() which returns a String[].)


Does File.ReadLines(filePath).First() close the file immediately?

Yes

...assuming by "immediately" you mean when the entire statement completes - not just the inner ReadLines() sub-expression.


  • Internally, File.ReadLines() returns an instance of ReadLinesIterator - which is an IEnumerable<T>.
  • When an IEnumerable<T> is iterated-over, C#/.NET uses IEnumerable<T>.GetEnumerator<T>() which returns an IEnumerator<T> which must be disposed after the program has finished iterating what it wants.
    • Because IEnumerator<T> instances must be disposed you're always encouraged to use foreach which handles this for you (instead of manually handling an IEnumerator<T> yourself).
      • foreach will also ensure the IEnumerator<T> is disposed if an exception is thrown inside the foreach loop body.
  • In this specific case ReadLinesIterator contains a StreamReader (which contains the open FileStream). When the ReadLinesIterator is disposed the internal StreamReader is closed, which in-turn closes the FileStream.
  • The .Frist() method is Linq's Enumerable.First( IEnumerable<T> source ).
    • Internally, Linq's First() does the same thing as calling foreach( T item in source ) and returning immediately inside the foreach - so the First method will dispose of the ReadLinesIterator for you.

Safety

I note that ReadLinesIterator is both an IEnumerator<T> and an IEnumerable<T> and it wraps an open StreamReader - which does mean that you do need to be careful when using ReadLines() to ensure that the IEnumerable<T> you see is actually iterated-over, otherwise you do risk leaking open file handles.

...this also means that if you're using a Linq method chain and an exception happens inside the Linq chain but outside any of Linq's internal try/catch/foreach/using blocks then you will leak an file handle that won't be closed until the GC finalizes the ReadLinesIterator... though I admit I struggle to think of when this could happen.

over 4 years ago · Santiago Trujillo Report

0

File.ReadLines returns IEnumerable<string>. Enumerable uses IEnumerator<string> to read lines, which is disposable.

Correct way to read first line and be resource-safe:

var lines = File.ReadLines(path);
string firstLine;
using (var it = lines.GetEnumerator())
{
    if (!it.MoveNext())
        throw new InvalidOperationException("File is empty");
    firstLine = it.Current;
}

UseLine(firstLine);

It's complicated but safe for sure

over 4 years ago · Santiago Trujillo Report

0

Best practice id to read file using stream reader.

var lines = new List<string>();

using (StreamReader reader = new StreamReader(@"C:\test.txt")) {
    var line = reader.ReadLine();

    while (line != null) {
        lines.Add(line);
        line = reader.ReadLine();
    }
}

Once the code move out of using statement, fill object will be disposed and wfile will be closed. File.ReadLines never closes the file.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!