user3040626
user3040626

Reputation: 21

The process cannot access the file 'D:\ebook\ff.jpg' because it is being used by another process

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)
     {
     }    
 }

Upvotes: 2

Views: 852

Answers (2)

Ringo
Ringo

Reputation: 3965

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.

Upvotes: 0

Monika
Monika

Reputation: 2210

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

Upvotes: 0

Related Questions