Wednesday, February 24, 2010

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




Please visit my new Web Site WWW.Codedisplay.com



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.

Asp.net 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:
using (System.Drawing.Image Img = System.Drawing.Image.FromFile(Server.MapPath("Images\\YourImageName.jpg"))) 
{ 
 // Do whatever you want 
}

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

0 comments:

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash