C# 该进程无法访问该文件,因为它正被另一个进程使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/877889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:08:40  来源:igfitidea点击:

The process cannot access the file because it is being used by another process

c#

提问by Greg Finzer

I have a set of nightly reports.

我有一组夜间报告。

Sometimes I get the exception:

有时我会遇到异常:

The process cannot access the file because it is being used by another process

该进程无法访问该文件,因为它正被另一个进程使用

How do I tell what process is holding on to the file? I am thinking that it is McAfee but I need to prove it.

我如何知道哪个进程正在保留文件?我认为这是迈克菲,但我需要证明这一点。

采纳答案by Greg Finzer

The problem was the MailMessage in .NET was holding on to the file attachments. I had to do a dispose on the attachment and then it worked.

问题是 .NET 中的 MailMessage 保留了文件附件。我必须对附件进行处理,然后它才起作用。

回答by samjudson

Filemonmight be the tool you are looking for.

Filemon可能是您正在寻找的工具。

(or even Process Monitorwhich apparently has replaced FileMon.)

(甚至显然已经取代了 FileMon 的Process Monitor。)

回答by Kamarey

here some tool(s) which tells and unlock locked files: http://ccollomb.free.fr/unlocker/

这里有一些工具可以告诉和解锁锁定的文件:http: //ccollomb.free.fr/unlocker/

回答by C. Ross

Use a tool like Process Exploreror Process Monitor.

使用Process ExplorerProcess Monitor 之类的工具。

回答by Carra

  1. Process Explorer> Find > Find Handle > type in your file name.
  2. Unlocker. Try to open it, if it has a lock, unlocker will open and show you the programs that lock it.
  1. Process Explorer> Find > Find Handle > 输入您的文件名。
  2. 解锁器。尝试打开它,如果它有锁,解锁器将打开并显示锁定它的程序。

回答by Greg Finzer

Confirmed. I had a similar problem and did what Greg said.

确认的。我遇到了类似的问题,并按照 Greg 所说的做了。

In finallyblock after sending the message, I just put this in there, and it released the file handles:

finally发送消息后的块中,我只是把它放在那里,它释放了文件句柄:

foreach(Attachment a in message.Attachments) a.Dispose();

回答by Rad_M

Attachment data = new Attachment(@"c:\filename");

'send email....

data.Dispose();    

回答by Gavin Crawley

GC.Collect() after the dispose should fix it

GC.Collect() 处理后应该修复它

回答by Baruch Atta

I was trying to start a Java program and got the message. Looking in Task Manager, there was already a Java task running (looping?) when I killed that process, my process was able to run.

我试图启动一个 Java 程序并收到消息。在任务管理器中查看,当我杀死该进程时,已经有一个 Java 任务正在运行(循环?),我的进程能够运行。

回答by Komengem

I just had the same problem but in my case i was trying to delete a .mdf file in Visul Studio 2012. There wasn't much information on the error given to help me solve this and so i came here looking for help. Then i realized that even though its a similar problem, it was rather a different process i had to deal with.

我只是遇到了同样的问题,但在我的情况下,我试图删除 Visul Studio 2012 中的一个 .mdf 文件。没有太多关于错误的信息来帮助我解决这个问题,所以我来这里寻求帮助。然后我意识到即使它是一个类似的问题,它也是一个我必须处理的不同过程。

What i did was try to delete the .mdf file in windows explorer and then i gave me an error that SQL Server 2008 R2 was in fact holding on to the file even though i had the database deleted. Once i new that, all i had to do was shutdown SQL Server 2008 R2 and tried to delete the file again and it worked.

我所做的是尝试在 Windows 资源管理器中删除 .mdf 文件,然后我给了我一个错误,即 SQL Server 2008 R2 实际上保留了该文件,即使我删除了数据库。一旦我更新了它,我所要做的就是关闭 SQL Server 2008 R2 并尝试再次删除该文件并且它起作用了。

If you need instructions on how to turn on and off SQL Server 2008 R2, see link below.

如果您需要有关如何打开和关闭 SQL Server 2008 R2 的说明,请参阅下面的链接。

Turn on and off SQL Server

打开和关闭 SQL Server