Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using VS2005 and sql server 2005,
At first I upload some files through html file upload control and save all those files in a folder. After that I am sending a mail which contains these files as attachments .
After sending mail I want to delete all the files from folder,but it gives exception
like file is used by another process .I am using fileupload1.dispose() method but it
still gives same exception.
Posted
Comments
DamithSL 9-Jul-12 12:53pm    
can you improve question with code ?
Trak4Net 9-Jul-12 13:40pm    
If you can give a code example of how you are saving the files to the file system and how you are loading them into the attachment. A common mistake is using the method System.IO.File.Open which returns a System.IO.FileStream and not closing the returned stream. While the thread that made the call is still alive and if the returned FileStream has not been closed you will get that exception.
Sergey Alexandrovich Kryukov 9-Jul-12 16:00pm    
Right, that is one of the common mistakes. I explained what to do in my answer, please see.
--SA

1 solution

This is easy to investigate.

First of all, the similar question was asked here many times, and from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up. To explore this possibility, please see my past answer:
Clearing a Handle in C#[^].

In this answer, pay attention for the using of the using statement which helps you to guarantee that appropriate file system object is properly disposed after use, not keeping the file locked.

In same cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the Sysinternals Suite. This set of utilities (formerly from Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[^].

The utility you need is "handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[^].

In your case, you use it with file name parameter:
handle.exe <file_name>


This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[^].

Good luck,
—SA
 
Share this answer
 
v2
Comments
Maciej Los 14-Sep-12 16:02pm    
Excellent, +5!
Sergey Alexandrovich Kryukov 14-Sep-12 16:04pm    
Thank you, Maciej.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900