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

Posted by Lirik on Stack Overflow See other posts from Stack Overflow or by Lirik
Published on 2010-04-23T20:10:30Z Indexed on 2010/04/23 20:13 UTC
Read the original article Hit count: 513

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about exception