简体   繁体   English

锁定目录并在File.Copy期间获取FileNotFoundException

[英]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 . 源文件存在,但是例如,如果在“ Windows资源管理器”中打开了另一个用户/计算机目录,则此操作将锁定,并且在复制过程中将获取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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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