File.Copy() 给出 system.IO.IOException: 该进程无法访问该文件..因为它正在被另一个进程使用。例外

发布于 2024-10-30 13:13:57 字数 131 浏览 8 评论 0原文

我在我的 asp.Net 应用程序中使用 File.Copy(..,..) 方法,它向我显示 system.IO.IOException: 该进程无法访问该文件..因为它正在被另一个进程使用。

任何解决办法

提前致谢

i am using File.Copy(..,..) method in my asp.Net app it is showing me system.IO.IOException: The process cannot access the file ..because it is being used by another process.

any work around for it

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

方觉久 2024-11-06 13:13:57

您必须 a) 停止正在使用该文件的进程,或者 b) 等待该文件不再被使用。

您应该阅读有关文件锁定的维基百科条目。请特别注意“在 Microsoft Windows 中”段落,您将更好地了解文件锁定的工作原理。

如果另一个程序使用共享模式参数打开该文件,该参数表明该文件不会被共享,即使是读取访问,那么您实际上无能为力 em>强制您的应用程序执行复制操作。

如果您热衷于“扮演上帝”,并且愿意杀死可能锁定您所关心的文件的进程,那么您可以尝试使用此中的信息找出哪个进程正在锁定您的文件线程:我如何找到哪个进程正在使用 .NET 锁定文件?

更好的策略可能是调查为什么您正在复制的文件被锁定,以及是否可以避免锁定冲突,通过对文件设置限制性文件权限,或者通过重新配置竞争文件锁定的软件。例如,如果防病毒程序偶尔扫描您的应用程序使用的文件夹结构,那么您也许可以告诉软件忽略该特定文件夹。

如果执行时间并不重要,您也许可以延迟或推迟复制操作。一个简单的策略是在下一次迭代中获取锁定的文件。另一种策略可能涉及监听事件,例如来自 FileSystemWatcher 类,其想法是 等待文件在 .NET 中解锁

You have to either a) stop the process which is using the file or b) wait until the file isn't being used anymore.

You should read this wikipedia entry on File Locking. Pay particular attention to the "In Microsoft Windows" paragraph where you will get a better idea about how file locking works.

If another program has opened the file with the sharing mode parameter stating that the file will not be shared even for read-access, then there is nothing you can really do to force the copying operation from your application.

If you are keen to "play god", and you are willing kill processes that may be holding a lock on the files you are concerned with, then you can attempt to find out which process is holding on to your file using the information in this thread: How do I find out which process is locking a file using .NET?

A better strategy is probably to investigate why the file that your are copying is being locked, and whether it is possible to avoid the lock conflict, by setting restrictive file permissions on the file(s), or by reconfiguring the software which is competing for the lock on the file. E.g. If an antivirus program occasionally scans a folder structure used by your app, then you can perhaps tell the software to ignore that particular folder.

If execution time is not critical you can perhaps delay or postpone your copy operation. A simple strategy would be to get-that-locked-file-on-the-next-iteration-instead. Another strategy could involve listening for events e.g. from the FileSystemWatcher class, with the idea to Wait until file is unlocked in .NET

⊕婉儿 2024-11-06 13:13:57

尝试一下

File.Open(name, FileMode.Open, FileAccess.Read, FileShare.None);

查看链接了解更多详细信息

try this

File.Open(name, FileMode.Open, FileAccess.Read, FileShare.None);

Check out this link for more details

情丝乱 2024-11-06 13:13:57

要找出锁定它的进程,您可以尝试 http://www.filehippo.com/download_unlocker/

To find out which process locking it you can try http://www.filehippo.com/download_unlocker/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文