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
562 stars 99 forks source link

File.Move on a file opened with FileShare.Delete succeeds but throws IOException. #479

Closed oguimbal closed 6 years ago

oguimbal commented 6 years ago

A small snippet being better than a long description:

var dir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(dir);

 // works
using (var x = File.Open(Path.Combine(dir, "sysio.txt"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete))
    System.IO.File.Move(Path.Combine(dir, "sysio.txt"), Path.Combine(dir, "sysio-moved.txt"));

// throws, but the move is successfuly performed (The process cannot access the file because it is being used by another process)
using (var x = File.Open(Path.Combine(dir, "alphafs.txt"), FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete))
    Alphaleonis.Win32.Filesystem.File.Move(Path.Combine(dir, "alphafs.txt"),Path.Combine(dir, "alphafs-moved.txt"));

File.Move is supposed to be successful when the file has been open with FileShare.Delete flag. In this case, File.Move sucessfully moves the file, but throws an exception (wtf ?).

NB: This issue is specific to File.Move, FileShare.Delete works perfectly fine with File.Delete()

Yomodo commented 6 years ago

Thanks!