alphaleonis / AlphaFS

AlphaFS is a .NET library providing more complete Win32 file system functionality to the .NET platform than the standard System.IO classes.
http://alphafs.alphaleonis.com/
MIT License
563 stars 99 forks source link

File.OpenRead method overloads do not use FileShare.Read #419

Closed elgonzo closed 6 years ago

elgonzo commented 6 years ago

As a drop-in replacement for System.IO.File, the method overloads for File.OpenRead (and File.OpenReadTransacted) should use FileShare.Read. However, they don't.

The AlphaFS File.OpenRead/OpenReadTransacted implementations call File.Open/OpenTransacted methods without specifying an FileShare.Read argument, which means the file is opened using FileShare.None).

The following example code using System.IO.File.OpenRead twice with the same file executes successfully without errors.

using (var s1 = System.IO.File.OpenRead(filePath))
{
    using (var s2 = System.IO.File.OpenRead(filePath))    // <--- works!
    {
    }
}

However, using the same example code with AlphaFS will not work:

using (var s1 = Alphaleonis.Win32.Filesystem.File.OpenRead(filePath))
{
    using (var s2 = Alphaleonis.Win32.Filesystem.File.OpenRead(filePath))    // <--- IOException
    {
    }
}

The second (nested) call of Alphaleonis.Win32.Filesystem.File.OpenRead will throw an IOException, due to the first (outer) File.OpenRead call not using FileShare.Read as expected.