AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 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 20864 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

Featured Resources:
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
The Art & Science of JavaScript - Free 100 Page Preview!
For intermediate JavaScript developers who want to take their JavaScript skills to the next level without sacrificing web accessibility or best practice.
100% Free Subscription until Stock lost.View complete list.
Sponsored by