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

In most of the cases specially in image manipulation time developers faced an error like:
The process cannot access the file because it is being used by another process.

The error happened because when you try to access an object of image or Font just immediately after you use it. Because the dot net framework does not destroy the resource before you calling. To resolve this problem you have to release the resource first & then you can use it again. To ensure freeing the unmanaged resource is a tough job but doable. provides us “using” statement to wrap up your respective code within using statement. This statement ensure that the object will be destroyed just after its scope even if any error occured within the block. So the solution is when you want to manipulate any image type object or Font or any file then wrap up all code within the using statement. The sample format is given below:

C# Code Sample:

using (System.Drawing.Image Img = System.Drawing.Image.FromFile(Server.MapPath("Images\\YourImageName.jpg"))) 
 // Do whatever you want 

VB.Net Code Sample:

'Do whatever you want 
Using Img As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath("Images\YourImageName.jpg"))
End Using

So it would be better to practice always wrap up your relevant code within “Using” statement.

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code