Ederaildo
Ederaildo

Reputation: 3

Locked directory and getting FileNotFoundException during File.Copy

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

Upvotes: 0

Views: 251

Answers (1)

Sushil Mate
Sushil Mate

Reputation: 583

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);
    }

Upvotes: 1

Related Questions