Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# File Exception: cannot access the file because it is being used by another process

I'm trying to download a file from the web and save it locally, but I get an exception:

C# The process cannot access the file 'blah' because it is being used by another process.

This is my code:

File.Create("data.csv");  // create the file
request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
request.Timeout = 30000;
response = (HttpWebResponse)request.GetResponse();

using (Stream file = File.OpenWrite("data.csv"), // <-- Exception here
    input = response.GetResponseStream())
{
    // Save the file using Jon Skeet's CopyStream method
    CopyStream(input, file);
}

I've seen numerous other questions with the same exception, but none of them seem to apply here. Any help?

Update:
Thanks for the answers! Removing the File.Create(...) fixed it!

One comment on the documentation of OpenWrite: it is a little misleading, the brief description says:

Opens an existing file for writing.

The detailed description says:

If the file exists, it is opened for writing at the beginning. The existing file is not truncated.

Update 2.0:
It looks like the discrepancy is between IntelliSense/F1 and the online documentation. I thought it should be the same since I allow F1 to connect to the web when it's displaying documentation.

like image 487
Kiril Avatar asked Apr 23 '10 20:04

Kiril


2 Answers

File.Create returns a FileStream - which you're not closing. That means you won't be able to open another stream writing to the same file until the finalizer has closed the existing stream.

Just get rid of the call to File.Create - File.OpenWrite will create it anyway. Alternatively, keep the FileStream around to write to:

using (Stream file = File.Create("data.csv"))
{
    request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
    request.Timeout = 30000;
    using (var response = (HttpWebResponse)request.GetResponse())
    using (Stream input = response.GetResponseStream())
    {
        // Save the file using Jon Skeet's CopyStream method
        CopyStream(input, file);
    }
}

Note that I'm also disposing of the WebResponse here, which you should do to make sure the connection is freed to the connection pool.

like image 168
Jon Skeet Avatar answered Oct 05 '22 08:10

Jon Skeet


looks like File.Create returns an open FileStream object

http://msdn.microsoft.com/en-us/library/aa328775(v=VS.71).aspx

try

using (FileStream fs = File.Create("data.csv"))

and leave off the first File.Create

like image 28
house9 Avatar answered Oct 05 '22 09:10

house9