El proceso no puede acceder al archivo 'D:\ebook\ff.jpg' porque está siendo utilizado por otro proceso

I cant delete an image after uploading in c# mvc.it shows an exception in File.delete(path)

Exception: The process cannot access the file 'D:\ebook\alignment11429852064.png' because it is being used by another process

fileupload process:

     public void UploadImage(HttpPostedFileBase file, FileImage upload)
     {
         try
         {
             string fileName = Path.GetFileNameWithoutExtension(file.FileName);
             string fileExtension = Path.GetExtension(file.FileName);
             string hashFile = fileName + DateTime.Now.GetHashCode();
             string path = Path.Combine(HttpContext.Current.Server.MapPath("D\ebook" + hashFile + fileExtension));
             upload.ImageFile = "D\ebook" + hashFile + fileExtension;
             file.SaveAs(path);
         }
         catch(exception e)
         {
         }
     }

FileImage is a model

here I have written for deleting file(image) function

public ProfileImage DiscardImage(FileImage image)
{
     try
     {
         if(File.Exists(HttpContext.Current.Server.MapPath(image.ImageFile)))
         {
             File.Delete(HttpContext.Current.Server.MapPath(image.ImageFile));
         }
     }
     catch(exception e)
     {
     }    
 }

preguntado el 27 de noviembre de 13 a las 07:11

Are you sure the image is not open in an image editor? -

"because it is being used by another process" is frequently a lie. It's almost always inside your own code and that you've got some other object lurking around waiting to be GCed that has a handle open to the file and that you should have called Dispose on by now. -

Oh the times I've had to work around this issue... deleting and renaming files in a web application is such a pain; anyone can hold the reference to that file - another request (such as an image preview, or a hundred users of your web), IIS (it handles caching and can hold it at will) and more. And to top that off, some changes to the files and directories can restart your whole application. -

2 Respuestas

The 'D:\ebook\alignment11429852064.png' image is open in another tab or another window. Close it and then try again.

respondido 27 nov., 13:07

Thank you for your suggestion.I have tried. But it doesn't work out for my code. Please suggest me alternative solution for that - user3040626

Try after restarting your PC then let me know. - Monika

But again I had a same problem - user3040626

Make sure your images that you have just downloaded are free or can not be used by any another functions in your own application. May be you just dispose them, So you can done with it.

respondido 27 nov., 13:08

Your code seen fine. I got the problem before in the difference situation. So I just try to avoid copying, saving,...etc and deleting at the same time when my app is opened. You know I mean, because at this time, my own app is using that files I can not delete or remove them. So what can I do is try to delete or remove in the next time I open the app. That's all my idea. Hope can help you! good luck. - Ringo

Gracias por su sugerencia - user3040626

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.