Cant Access File because it is being used by another process


Question

I have an ASP.NET program where i am downloading a file from web using DownloadFile method of webClient Class and the do some modifications on it. then i am Saving it to another folder with a unique name.When I am getting this error

The process cannot access the file 'D:\RD\dotnet\abc\abcimageupload\images\TempStorage\tempImage.jpg' because it is being used by another process

Can anyone tell me how to solve this.

1
3
9/2/2011 8:50:53 AM

Accepted Answer

Generally, I think your code should looking something like this.

WebClient wc = new WebClient();
wc.DownloadFile("http://stackoverflow.com/Content/Img/stackoverflow-logo-250.png", "Foo.png");
FileStream fooStream;
using (fooStream = new FileStream("foo.png", FileMode.Open))
{
    // do stuff
}
File.Move("foo.png", "foo2.png");
6
11/26/2008 3:55:42 PM

I've had very good success using the tools from SysInternals to track which applications are accessing files and causing this kind of issue.

Process Monitor is the tool you want - set it up to filter the output to just files in the folder you're interested in, and you'll be able to see every access to the file.

Saves having to guess what the problem is.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon