[英]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.
C#进程无法访问文件“ blah”,因为它正在被另一个进程使用。
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! 删除
File.Create(...)
修复了它!
One comment on the documentation of OpenWrite
: it is a little misleading, the brief description says: 关于
OpenWrite
文档的一则评论:简短描述说,这有点误导:
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: 更新2.0:
It looks like the discrepancy is between IntelliSense/F1 and the online documentation. 看来IntelliSense / F1与联机文档之间存在差异。 I thought it should be the same since I allow F1 to connect to the web when it's displaying documentation.
我认为应该相同,因为在显示文档时允许F1连接到Web。
File.Create
returns a FileStream
- which you're not closing. File.Create
返回一个FileStream
您没有关闭它。 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. 刚刚摆脱调用的
File.Create
- File.OpenWrite
无论如何都会创建它。 Alternatively, keep the FileStream
around to write to: 或者,保持
FileStream
左右写入:
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. 请注意,我还在这里处理
WebResponse
,您应该这样做以确保将连接释放到连接池中。
looks like File.Create returns an open FileStream object 看起来像File.Create返回一个打开的FileStream对象
http://msdn.microsoft.com/en-us/library/aa328775(v=VS.71).aspx http://msdn.microsoft.com/zh-CN/library/aa328775(v=VS.71).aspx
try 尝试
using (FileStream fs = File.Create("data.csv"))
and leave off the first File.Create 并保留第一个File.Create
The problem is that the File.Create method actually opens the file and returns a FileStream object. 问题在于File.Create方法实际上会打开文件并返回FileStream对象。 It won't close the file until the object is garbage collected (which will happen at an indeterminate time).
在对象被垃圾收集之前,它不会关闭文件(这将在不确定的时间内发生)。 The FileStream object still gets created, regardless of whether or not you use it.
不管您是否使用FileStream对象,仍然会创建它。 Try this:
尝试这个:
using (Stream file = File.Create("data.csv"),
input = response.GetResponseStream())
{
// Save the file using Jon Skeet's CopyStream method
CopyStream(input, file);
}
First. 第一。 File.Create will return a stream that you should use for accessing the file.
File.Create将返回用于访问文件的流。
Second, if that doesn't resolve the issue, if you google who lock
you will find a program that let's you determine what process is accessing a file at the same time. 其次,如果那不能解决问题,那么如果您用谷歌
who lock
您,您将找到一个程序,让您确定同时正在访问文件的进程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.