简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM