0

I´m trying to copy a shared file to local copy:

File.Copy("\\sharedmachine\directory\file.exe", "\\localmachine\directory\file.exe", true);

The source file exists but if another user/machine is opened directory in the "Windows Explorer" for example, this operation lock and during the copy i´m getting a System.IO.FileNotFoundException.

There are some way to copy file even if someone open the directory in another machine?

Thanks

Ederaildo
  • 3
  • 2

1 Answers1

1

opening the file as read-only and then writing it to the destination, so that apps accessing the file is not blocked.

using (var from = File.Open("sourcePath", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (var to = File.OpenWrite("destPath"))
    {
        from.CopyTo(to);
    }
Sushil Mate
  • 583
  • 6
  • 14
  • 1
    Maybe provide some details as to why you believe your code is correct, or what yours solves that the OP's does not. – Anthony Horne Jun 30 '17 at 13:28
  • @AnthonyHorne don't you think code is self explanatory? – Sushil Mate Jun 30 '17 at 13:36
  • 1
    To me, but maybe not to the OP that has 3 points. Adding a one-line comment to say that you are opening the file as read-only and then writing it to the destination, so that apps accessing the file is not blocked - would be helpful for others to understand. It is your answer and you should provide the best answer you can to move the whole community forward with you. – Anthony Horne Jun 30 '17 at 13:40
  • 1
    @AnthonyHorne agree. Added your explanation :) – Sushil Mate Jun 30 '17 at 13:58