File.ReadLines without locking it?

17,531

Solution 1

No... If you look with Reflector you'll see that in the end File.ReadLines opens a FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, FileOptions.SequentialScan);

So Read-only share.

(it technically opens a StreamReader with the FileStream as described above)

I'll add that it seems to be child's play to make a static method to do it:

public static IEnumerable<string> ReadLines(string path)
{
    using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 0x1000, FileOptions.SequentialScan))
    using (var sr = new StreamReader(fs, Encoding.UTF8))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            yield return line;
        }
    }
}

This returns an IEnumerable<string> (something better if the file has many thousand of lines and you only need to parse them one at a time). If you need an array, call it as ReadLines("myfile").ToArray() using LINQ.

Please be aware that, logically, if the file changes "behind its back (of the method)", how will everything work is quite undefined (it IS probably technically defined, but the definition is probably quite long and complex)

Solution 2

File.ReadLines() will lock the file until it finishes.

Share:
17,531
Felipe Pessoto
Author by

Felipe Pessoto

.Net Developer

Updated on June 06, 2022

Comments

  • Felipe Pessoto
    Felipe Pessoto almost 2 years

    I can open a FileStream with

    new FileStream(logfileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    

    Without locking the file.

    I can do the same with File.ReadLines(string path)?

  • abatishchev
    abatishchev about 13 years
    as far as ReSharper is a profiling tool, Reflector should be used in such case
  • Jon Skeet
    Jon Skeet about 13 years
    @abatishchev: I wouldn't describe ReSharper as a profiling tool. I believe it can do decompilation now, too (possibly only in a beta release).
  • CodesInChaos
    CodesInChaos about 13 years
    Why do you call your method ReadAllLines if it's behavior corresponds to ReadLines and not ReadAllLines?
  • xanatos
    xanatos about 13 years
    @abatishchev I meant Reflector... My error... (I use both, but for different purposes). But from the next version (or so), Resharper will contain a "Reflector-like" module (this is because Reflector should have become pay-only from March). You can even use ILSpy or other programs.
  • xanatos
    xanatos about 13 years
    @CodeInChaos because the Visual Studio didn't tag it as an error or as a warning (clearly it can't divine stupidity)... Is it enough? :-) I made the correction.
  • Mukesh Adhvaryu
    Mukesh Adhvaryu about 8 years
    Answer should represent solution or at least should seem to be pointing toward that direction. Answer seeker already knows the fact you asserted so it does not do anything helpful.
  • xanatos
    xanatos over 7 years
    @ms007 The ReadLines uses a ReadLinesIterator.CreateIterator(path, Encoding.UTF8) that creates a new StreamReader(path, encoding) that in turn creates an internal FileStream. So no big difference.