AspdotnetCodes.com
Voted 'Best ASP.NET Host' for 2008 – DiscountASP.NET
 
Articles Subscribe for our Articles Updates
Books
Resources
Downloads
Free Tech Magazines
Archives
Softwares
Newsletter
Suggest Us
Link to Us
 Solution: The process cannot access the file because it is being used by another process
Posted by Moderator1 on  6/28/2008 11:02:13 PM 277 Views
 
This is an Asp.Net error occurs when you try to access a File object immediately after you use it. This error means the CLR otherwise the .NET Framework doesn’t release the resource yet after it is being used successfully. Or this error can even come when some problem is encountered in between processing that file. The way to handle this is simple.

The ‘using’ statement helps to overcome this problem. File and Font classes are managed objects that can access unmanaged resources such as Images, Fonts, or other files in the server. When we create instances for these classes, it is generally better to instantiate the object in the using statement and limit its scope to the using block. The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object and releases the resources used by the objects perfectly.

A general syntax is given below

using (System.Drawing.Image Img = System.Drawing.Image.FromFile(Server.MapPath("Images\\TestImage.jpg")))
{
// Do some operation here
}

Post New Tips/Tricks View all Tips/Tricks

Sponsored by