I'm trying to write some code that deletes an image off the hard-disk once the user clicks on some delete button. Sometimes I get the following exception and sometimes I do not. And when I actually do, if I try to delete it again, it does work most of the time.
This is the exception:
System.IO.IOException: The process cannot access the file because it is being used by another process.
I guess I should provide some details on what is happening exactly:
- User uploads an image, which is then displayed on the screen so the user can see what he/she has just uploaded.
- A delete button is shown to the user in case he/she decides that they do not really want to upload this image.
- When the user clicks the delete button, I call a method that deletes the image and all of its previously created thumbs.
- Finally, the image is remove from the screen and the user can upload other images.
I'm not sure how I could solve this problem because the exception does not provide any information about which other process is holding onto the file. Any ideas?