2

I made a system where user can upload a file (image) to a server and server saves it. All is good, but when I want to delete the files uploaded by user, I get an exception saying:

the process cannot access the file because it is being used by another process

This is the code for saving the file:

HttpFileCollection files = httpRequest.Files;

for (int i = 0; i < files.Count; i++) {
    var postedFile = files[i];

    // I tried this one before, but I read that I should .Dispose() files, therefore
    // I settled to the other, uncommented solution (however, both of them do the same thing)
    //postedFile.SaveAs(filePath);
    using (FileStream fs = File.Create(filePath)) {
        postedFile.InputStream.CopyTo(fs);
        postedFile.InputStream.Close();
        postedFile.InputStream.Dispose();
        fs.Dispose();
        fs.Close();
    }
}

The deleting of files is quite simple. In a method called Delete, I call this method:

...
File.Delete(HttpContext.Current.Server.MapPath(CORRECT_PATH_TO_FILE));
...

Any suggestions on how to solve this?

Thanks

9
  • What type is postedFile? Commented May 13, 2014 at 14:54
  • 3
    If you don't want the file, why bother to save? You could use a MemoryStream instead and dispose it. or use the InputStream property of the posted file. Commented May 13, 2014 at 14:55
  • @KeithPayne, it's an HttpPostedFile per the MSDN documentation. Commented May 13, 2014 at 14:55
  • 1
    @user1680977: the answer, albeit vague, is that somewhere in the workflow you left a stream open to the file. That may not be in the upload code, there may be something else in the workflow doing it. Commented May 13, 2014 at 15:05
  • 1
    @MichaelPerrenoud Hehe. Although your answer was very vague, it actually helped me to solve the problem. I had another class that was reading the files and then serving them to user ... and never disposed them. Thanks a lot for your help! Commented May 13, 2014 at 15:09

2 Answers 2

1

Just as Michael Perrenoud suggested me in the comment to my main question, I was also opening the file in another class and not disposing it when done with working with it. Problem is therefore solved. Thanks!

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

Comments

0

Where are you trying the file delete method? As part of the loop? If so, it is natural to have it locked. If outside of the loop, then it is a different problem (perhaps not garbage collected yet?).

To avoid the loop problem, gather a list of locations you are going to delete (declare outside of the loop, can be populated within) and then delete in another "clean up" loop (another method is even better for reusability).

NOTE: Close() before Dispose() not the other way around. You actually do not have to do both, as Dispose() should always handle making sure everything is clean (especially in .NET framework uses of IDisposable), but I don't see any harm in Close() followed by Dispose().

1 Comment

Thanks for help, I found the mistake by opening the file in one more place and not disposing it. Couldn't see this simple mistake in hours :P

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.