Solution: The process cannot access the file [filename] because it is being used by another process.

Don't you just hate it when you get a programming error that pisses away your entire day? I had one of those errors today. I'm going to share with you my findings so you only piss away half-of-a-day. :-P

I've been writing a quote generating web application for .NET 2.0. This application uses Crystal Reports XI to generate a printer friendly quote to PDF using the ReportDocument.ExportToDisk method. That PDF gets emailed to a list of people. The application seemed to be working great until today. I was writing some code to delete those PDF files and I got this error:
The process cannot access the file '[filename]' because it is being used by another process.

I started troubleshooting by assuming the CrystalReportSource.ReportDocument had inadvertently kept the exported files open. I start using dispose methods against the crystal objects, GC.Collect, etc... I started looking for tids on Business Objects' web site about this issue, or discussions about known bugs, etc. I brought out the BIG GUNS and downloaded SysInternals FileMon from Microsoft to see exactly what was going on. Here is a summary of the results.


w3wp.exe:5800 CREATE C:\QMS\Quotes\1005.pdf
w3wp.exe:5800 WRITE C:\QMS\Quotes\1005.pdf
w3wp.exe:5800 CLOSE C:\QMS\Quotes\1005.pdf
w3wp.exe:5800 OPEN C:\QMS\Quotes\1005.pdf
w3wp.exe:5800 READ C:\QMS\Quotes\1005.pdf

As you can see, it created the PDF, it wrote the PDF, it closed the PDF (expected). Then, there was an unexpected Open, Read, without close immediately after the file was created. Obviously, we've found the problem. I made a last ditch effort to export the PDF to a temporary location, use System.IO.File to copy the file to the actual location, and that file was STILL being opened without a corresponding CLOSE.

HERE WAS THE SOLUTION:
I looked a little lower in my code and found a routine that emails the file using Microsoft's System.Net.Mail.MailMessage. AHHHHH HA! The Attachments.Add method opened the file to attach it to the MailMessage. After using the System.Net.Mail.SmtpClient to send the message, the message object was never cleared, even after garbage collection. By changing my code to do this:


Dim msg as New System.Net.Mail.MailMessage()
Try
... code to create message and attach files
... code to send with smtpclient
Catch
... whatever

Finally
msg.Dispose() ' This was the solution!

End Try

... the file was then closed during the disposal of the message object, and I became a happy programmer.

So, if you get this error, start thinking about EVERY process that touches your files and experiment with Dispose. I highly recommend FileMon to pinpoint where the code went wrong.

Happy Programming!

Comments

Gracias, me funcion├│.

Gracias, me funcion├│.

HERO

HERO

Thanks a lot....it is

Thanks a lot....it is working...

I stuck on same It's

I stuck on same
It's Greatttttttttttt. Thanks..

Greatttttttttttttttttt!!!!

Greatttttttttttttttttt!!!!

Thanks a lot. This is helpful

Thanks a lot. This is helpful for me.

thanks aloooott ...you saved

thanks aloooott
...you saved my time...

Genius. Thank you so much!!!

Genius.
Thank you so much!!! you save me!

thanks a lot this is working

thanks a lot this is working for me..

Thanks a lot

Thanks a lot

Genius. Helped from my days

Genius. Helped from my days of frustration.

Thanks! i was making my ass

Thanks!

i was making my ass flat sitting here and scratching my head trying to solve this problem!!

bye

Exactly the problem I've been

Exactly the problem I've been having this morning, thanks for the solution!

Thanks a lot.I have been

Thanks a lot.I have been working on the same from morning!!! :-)

Thanks a lot dude. You save

Thanks a lot dude. You save my life !!

Awesome. I found your

Awesome.

I found your response after researching for 3 hours. This fixed it.

Thanks a lot for sharing!!!!

Thanks a lot for helping

Thanks a lot for helping me...............

Brilliant! You nailed a

Brilliant! You nailed a problem that was vexing me!! Thanks for posting!!

Thanks a lot! That was my

Thanks a lot! That was my same problem!

Thanks for your help. I

Thanks for your help. I really appreciate it.

thank you

thank you

Thank you !! really saved my

Thank you !! really saved my day ;)

Dude, you saved my lunch!!

Dude, you saved my lunch!!

Imagine that: Doing what you

Imagine that: Doing what you are supposed to do (calling Dispose() on *every* object you use that implements it) results in correctly functioning code!

**Always** call Dispose() when you are done using an object that implements it. Always.

SAVED MY DAY!!! Very helpful

SAVED MY DAY!!! Very helpful article.

Thanks a ton Brian. I was

Thanks a ton Brian. I was facing exactly the same issue. You saved my night :)

Cheers - Ashish.

Thanks Dude, It really helped

Thanks Dude, It really helped me out I'd the same problem.

Thanks alot.

Thanks from Denmark, this

Thanks from Denmark, this solved my problem :)

/Thomas

Very scary... I am writing a

Very scary... I am writing a quote generation system that emails the quotes... very mucy like you describe. Unfortunately my problem is different - the report is emailed by a separate process at a slightly later time. Also the problem is intermittant. Only happens 1 in a hundred times.

Thanks, U help me a lot

Thanks, U help me a lot

thanks....It helped me a

thanks....It helped me a lot....

thank you so much

thank you so much

Thanks so much for pointing

Thanks so much for pointing this out. I was banging my head against the desk for a couple hours until I this post.

Also, I had to move my Export-Csv statement above the new-object Net.Mail.Attachment statement or it would open a file handle on it and I couldn't overwrite the old .csv file.

thanks....It helped me a

thanks....It helped me a lot....

Thanks, its helped me....

Thanks, its helped me....

Solved a problem I've been

Solved a problem I've been wrestling with all day!

Many thanks

Nice 1 Brian that worked a

Nice 1 Brian that worked a treet, thank you very much for your time.

Thank you soooo much. I'm so

Thank you soooo much. I'm so used to trawling through Crystal code for unexpected errors I'd completely overlooked the emailing section!!

sorry, it still shows the

sorry, it still shows the error even I put the msg.Dispose() in finally.

Please help.

Your post here really helped

Your post here really helped me out. Thanks!

You helped me. Thanks chap.

You helped me. Thanks chap.

Thanks a lot dude

Thanks a lot dude