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.

crthompson
  • 15,653
  • 6
  • 58
  • 80
Greg Finzer
  • 6,714
  • 21
  • 80
  • 125
  • 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 '09 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. – Greg Finzer May 20 '09 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. – Greg Finzer Apr 06 '11 at 14:40

10 Answers10

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.

Greg Finzer
  • 6,714
  • 21
  • 80
  • 125
9

Use a tool like Process Explorer or Process Monitor.

C. Ross
  • 31,137
  • 42
  • 147
  • 238
  • 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 09 '11 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 09 '11 at 12:06
  • i have made this question but i didnt got an appropriate answer. the link is http://stackoverflow.com/questions/5152841/file-renaming-problem – safi Mar 09 '11 at 14:03
3
Attachment data = new Attachment(@"c:\filename");

'send email....

data.Dispose();    
Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
Rad_M
  • 31
  • 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();
Markus Amalthea Magnuson
  • 8,415
  • 4
  • 41
  • 49
  • @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 09 '11 at 10:44
1

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

Kamarey
  • 10,832
  • 7
  • 57
  • 70
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.

Baruch Atta
  • 421
  • 1
  • 5
  • 16
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

Komengem
  • 3,662
  • 7
  • 33
  • 57
0

GC.Collect() after the dispose should fix it

Gavin Crawley
  • 335
  • 3
  • 14
  • 25
  • 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 02 '12 at 21:38
0

Filemon might be the tool you are looking for.

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

tjd
  • 4,064
  • 1
  • 24
  • 34
samjudson
  • 56,243
  • 7
  • 59
  • 69
  • 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 09 '11 at 10:45
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.
Carra
  • 17,808
  • 7
  • 62
  • 75
  • 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 09 '11 at 10:44