C# File Exception: cannot access the file because it is being used by another process
- by Lirik
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?