1

I get this error

The process cannot access the file '..\Images\Temp\6574_1212665562989_1419270107_30610848_6661938_n.jpg' because it is being used by another process.

When I tried this:

try
{
    var file = Request.Files["FileProfilePicture"];
    file.SaveAs(Server.MapPath("~/Images/Temp/" + file.FileName));
    Bitmap imageOrj = new Bitmap(System.Web.HttpContext.Current.Server.MapPath("~/Images/Temp/" + file.FileName));
    Image imageBig = ResizeImage.Resize(imageOrj, 100, 100);
    imageBig.Save(System.Web.HttpContext.Current.Server.MapPath("~/Images/ProfilePicBig/" + file.FileName));
    Image imageSmall = ResizeImage.Resize(imageOrj, 50, 50);
    imageSmall.Save(System.Web.HttpContext.Current.Server.MapPath("~/Images/ProfilePicSmall/" + file.FileName));

    string[] files = System.IO.Directory.GetFiles(Server.MapPath("~/Images/Temp/"));
    foreach (string pathFile in files)
    {
        System.IO.File.Delete(pathFile);
    }


    return RedirectToAction("Index", "Author");
}
catch (Exception e)
{
    ModelState.AddModelError("", "Kullanıcı bilgileri güncellenirken bir hata oluştu. Lütfen daha sonra tekrar deneyin." + e.Message);
}

How can I fix it. Or another better way to keep images as a temp. Should I keep files in a temp folder?

Thanks

3 Answers 3

1

Did you make sure you do not have the file in temp open somewhere else?

This error also occurs when you had an error in a previous run and the file has not been closed. You can try a different file name in this case or just delete the unclosed file from the OS side.

I hope this helps...

Otherwise, I usually use a similar way of doing temp files...

Edit: By the comments, it seems that the resolution for the issue above was the following: whenever handled in a try-catch block, the file objects might not get closed down if it's not handled in the catch node. In this specific case the imageOrj object caused the problem, so it's advised to use an imageOrj.Dispose() after the Bitmap editing is finished.

Sign up to request clarification or add additional context in comments.

4 Comments

How can I close file after saving.
I'm not sure the FileStream().Close() would solve it, it not being a FileStream, but an HttpPostedFileBase. But the SaveAs method should not open the file, that just literally creates a copy of it (or flushes the HttpPostedFile into it), so it must be your Bitmap object that throws a wobble... meaning that that's the one keeping the file open, so I guess you can use an imageOrj.Dispose() after you've finished with the Bitmap...
I think the link in Jakub's post is a good example, so if that solves your problem, you can accept that...
imageOrj.Dispose() is the answer. If you write as answer. I wil Accept yours. Thanks
1

You don't need to save the temp file. You can create the bitmap in memory and populate it with the request stream. There is no need to save it to the disk.

Comments

-1

You seem to delete more files than you create in your deletion loop. You are attempting to delete every single file under ~/Images/Temp/, this might create conflicts (ie data races between different requests). Delete only the files you just created.

2 Comments

I think, It is not important.
how is this not important? imagine two concurrent requests, both creating differente files, one of the request will finish earlier and try to delete files already in use by the first request. There is clearly data race here on the temp directory :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.