20

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)?

Felipe Fujiy Pessoto
  • 6,492
  • 8
  • 40
  • 71
  • 5
    [The documentation](http://msdn.microsoft.com/en-us/library/dd383503.aspx) does not say anything about the locking behavior of ReadLines. Thus, even if it should turn out that the implementation of the .net Framework you use does not lock the file, it's not something that you can rely on when developing your application. – Heinzi Mar 17 '11 at 11:40

2 Answers2

43

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)

hammar
  • 134,089
  • 17
  • 290
  • 377
xanatos
  • 102,557
  • 10
  • 176
  • 249
  • as far as ReSharper is a profiling tool, Reflector should be used in such case – abatishchev Mar 17 '11 at 11:49
  • @abatishchev: I wouldn't describe ReSharper as a profiling tool. I believe it can do decompilation now, too (possibly only in a beta release). – Jon Skeet Mar 17 '11 at 11:50
  • 1
    Why do you call your method `ReadAllLines` if it's behavior corresponds to `ReadLines` and not `ReadAllLines`? – CodesInChaos Mar 17 '11 at 11:51
  • 1
    @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 Mar 17 '11 at 11:52
  • @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. – xanatos Mar 17 '11 at 11:53
  • @ms007 The [`ReadLines`](https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/IO/File.cs#L507) uses a [`ReadLinesIterator.CreateIterator(path, Encoding.UTF8)`](https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/IO/ReadLinesIterator.cs#L99) that creates a [`new StreamReader(path, encoding)`](https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/IO/StreamReader.cs#L191) that in turn creates an internal `FileStream`. So no big difference. – xanatos Dec 15 '16 at 14:37
1

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

Aliostad
  • 76,981
  • 19
  • 152
  • 203
  • 3
    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. – Mukesh Adhvaryu May 04 '16 at 05:55