24

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.

3
  • 2
    Just a thought, it's probably not your Virus protection. It never has been in my experience. In fact it's usually my code. Do make sure you've got "using" everywhere you open a file.
    – C. Ross
    May 18, 2009 at 15:19
  • That other post on Stack Overflow is a lock on a dll. This is a file lock on a text file. I do have a using statement. May 20, 2009 at 15:00
  • I would check the folder access rights for whatever principal you are using for your process. It may not have the rights to move it, just read only access. Apr 6, 2011 at 14:40

10 Answers 10

19

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.

0
9

Use a tool like Process Explorer or Process Monitor.

3
  • I am trying to rename a file in c#,i simply use file.move(source,destination) but every time i try i get an error the file is used by another process. i am renaming a through web interface using asp.net, iis6. thanks
    – safi
    Mar 9, 2011 at 10:45
  • @safi You need to make this a new question. You're welcome to comment me a link to it ...
    – C. Ross
    Mar 9, 2011 at 12:06
  • i have made this question but i didnt got an appropriate answer. the link is stackoverflow.com/questions/5152841/file-renaming-problem
    – safi
    Mar 9, 2011 at 14:03
3
Attachment data = new Attachment(@"c:\filename");

'send email....

data.Dispose();    
1

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

3
1

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

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

foreach(Attachment a in message.Attachments) a.Dispose();
1
  • @Mike I am trying to rename a file in c#,i simply use file.move(source,destination) but every time i try i get an error the file is used by another process. i am renaming a through web interface using asp.net, iis6. thanks
    – safi
    Mar 9, 2011 at 10:44
0
  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
  • I am trying to rename a file in c#,i simply use file.move(source,destination) but every time i try i get an error the file is used by another process. i am renaming a through web interface using asp.net, iis6. thanks
    – safi
    Mar 9, 2011 at 10:44
0

GC.Collect() after the dispose should fix it

1
  • this works in my case since the disposes aren't happening quickly enough or strictly. it just feels bad to do. there has to be a better way.
    – Steve
    Feb 2, 2012 at 21:38
0

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.

0

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.

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.

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

Turn on and off SQL Server

0

Filemon might be the tool you are looking for.

(or even Process Monitor which apparently has replaced FileMon.)

1
  • I am trying to rename a file in c#,i simply use file.move(source,destination) but every time i try i get an error the file is used by another process. i am renaming a through web interface using asp.net, iis6. thanks
    – safi
    Mar 9, 2011 at 10:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.