如何在另一个进程正在使用文件时复制该文件
是否可以复制另一个进程同时正在使用的文件?
我问是因为当我尝试使用以下代码复制文件时会引发异常:
System.IO.File.Copy(s, destFile, true);
引发的异常是:
该进程无法访问文件“D:\temp\1000000045.zip”,因为该文件正在被另一个进程使用。
我不想创建新文件,我只想复制或删除它。这可能吗?
Is it possible to copy a file which is being using by another process at the same time?
I ask because when i am trying to copy the file using the following code an exception is raised:
System.IO.File.Copy(s, destFile, true);
The exception raised is:
The process cannot access the file 'D:\temp\1000000045.zip' because it is being used by another process.
I do not want to create a new file, I just want to copy it or delete it. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一个示例(注意:我刚刚组合了两个谷歌结果,您可能需要修复小错误;))
重要的部分是打开 FileStream 时的
FileShare.ReadWrite
。我使用类似的代码打开并读取 Excel 文档,同时 Excel 仍处于打开状态并阻止文件。
An Example (note: I just combined two google results, you may have to fix minor errors ;))
The important part is the
FileShare.ReadWrite
when opening the FileStream.I use a similar code to open and read Excel documents while excel is still open and blocking the file.
要创建由 Windows 上的另一个进程读和/或写锁定的文件副本,最简单(也可能是唯一)的解决方案是使用卷影复制服务 (VSS)。
卷影复制服务很复杂并且难以从托管代码调用。幸运的是,一些优秀的人已经创建了一个 .NET 类库来完成此任务。查看 CodePlex 上的 Alpha VSS 项目:http://alphavss.codeplex.com。
编辑
与 CodePlex 上的许多项目一样,Alpha VSS 已迁移到 GitHub。该项目现在位于:https://github.com/alphaleonis/AlphaVSS。
To create a copy of a file that is read- and/or write-locked by another process on Windows, the simplest (and probably only) solution is to use the Volume Shadow Copy Service (VSS).
The Volume Shadow Copy Service is complex and difficult to call from managed code. Fortunately, some fine chaps have created a .NET class library for doing just this. Check out the Alpha VSS project on CodePlex: http://alphavss.codeplex.com.
EDIT
As with many of the projects on CodePlex, Alpha VSS has migrated to GitHub. The project is now here: https://github.com/alphaleonis/AlphaVSS.
那么,另一种选择是使用 Process 类将锁定的文件复制到某处,并调用 CMD 来使用“复制”命令。在大多数情况下,“复制”命令将能够复制文件,即使该文件正在被另一个进程使用,从而绕过 C# File.Copy 问题。
例子:
Well, another option is to copy the locked file somewhere by using Process class and invoke CMD to use the "copy" command. In most cases the "copy" command will be able to make a copy of the file even if it is in use by another process, bypassing the C# File.Copy problem.
Example:
FileInfo 的 CopyTo 方法将现有文件复制到新文件,从而允许覆盖现有文件。这就是为什么它不会中断现有文件的处理过程。
The CopyTo method of FileInfo copies an existing file to a new file, allowing the overwriting of an existing file. That's why it doesn't break process working on existing file.
您应该探索并找出哪个进程正在阻止该文件。如果这个过程不是你的,你就没有办法解决问题。当然,您可以应用一些技巧并手动释放文件锁,但这种方法很可能会导致意想不到的后果。如果该进程是您的,请尝试解锁该文件或使用共享读取访问权限锁定该文件。
[编辑]
找出阻止进程的最简单方法是使用 Process Explorer。启动并在
Find->Find Handle or DLL...
对话框中输入文件名。在搜索结果中,您将能够看到哪个进程正在阻止您的文件。您还可以在 C# 中完成此工作,请检查:什么进程锁定文件?。还
You should explore and find out which process is blocking the file. If this process is not yours, you have no way to solve the problem. Of course, you can apply some hacks and manually free the file lock but it's most likely that this approach will lead to unsuspected consequences. If the process is yours, try to unlock the file or lock it with share read access.
[EDIT]
The most easier way find out blocker process would be to use Process Explorer.Launch it and enter the file name in
Find->Find Handle or DLL...
dialog box. In the search results, you would be able to see which process is blocking your file.You also can do this job in C# check this: What process locks a file?. Also
尝试:
Try: