FileHandle prevents File.Move

107 views Asked by At

I am trying to figure out why the following code prevents overwriting of the file used in the filestream.

This only happens when using Cut and Paste, Copy and Paste works weirdly enough

var fs = new FileStream(
    File.OpenHandle(
        path,
        mode: FileMode.Open,
        access: FileAccess.Read,
        share: FileShare.Delete | FileShare.ReadWrite
        ), FileAccess.Read);

This is the error shown when Using Cut & Paste enter image description here

This snippet demonstrates the error:

Both files exist, no other applications are using them either

var path = @"C:\temp\test\test.log";
var pathCopy = @"C:\temp\test.log";

var fs = new FileStream(
    File.OpenHandle(
        path,
        mode: FileMode.Open,
        access: FileAccess.Read,
        share: FileShare.Delete | FileShare.ReadWrite
        ), FileAccess.Read);
    

File.Copy(pathCopy, path,true); //Works
File.Move(pathCopy, path, true); //Does NOT work -> AccessError
0

There are 0 answers