C# 无法访问文件,因为它正被另一个进程使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/321077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cant Access File because it is being used by another process
提问by Shyju
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
我有一个 ASP.NET 程序,我正在使用 webClient 类的 DownloadFile 方法从 Web 下载文件,并对其进行一些修改。然后我将它保存到另一个具有唯一名称的文件夹中。当我收到此错误时
The process cannot access the file 'D:\RD\dotnet\abc\abcimageupload\images\TempStorage\tempImage.jpg' because it is being used by another process
该进程无法访问文件“D:\RD\dotnet\abc\abcimageupload\images\TempStorage\tempImage.jpg”,因为它正被另一个进程使用
Can anyone tell me how to solve this.
谁能告诉我如何解决这个问题。
采纳答案by Joel Meador
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");
回答by EBGreen
Are you explicitly closing the file stream after you make your changes?
进行更改后是否明确关闭文件流?
回答by Bevan
I've had very good success using the tools from SysInternals to track which applications are accessing files and causing this kind of issue.
我使用 SysInternals 的工具来跟踪哪些应用程序正在访问文件并导致此类问题取得了非常大的成功。
Process Monitoris 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.
Process Monitor是您想要的工具 - 将其设置为将输出过滤为您感兴趣的文件夹中的文件,您将能够看到对该文件的每次访问。
Saves having to guess what the problem is.
无需猜测问题所在。
回答by Moulde
I dont know if this will solve your problem..
不知道这样能不能解决你的问题..
I got the exact same error when writing to a text file and then trying to open it afterwards.
写入文本文件然后尝试打开它时,我遇到了完全相同的错误。
It was solved by flushing the writer and then closing it after writing to the file..
它是通过刷新写入器然后在写入文件后关闭它来解决的。
回答by Moulde
try the following, set your filestream to Asynchronus mode (3rd parameter)
尝试以下操作,将文件流设置为异步模式(第三个参数)
FileStream myStream = File.Create(fileName, results.Length,FileOptions.Asynchronous);
//make sure you close the file
myStream.Write(results, 0, results.Length);
myStream.Flush();
myStream.Close();
myStream.Dispose();
if this fails reset the attributed of the file b4 you access it
如果此操作失败,请重置您访问它的文件 b4 的属性
File.SetAttributes(Server.MapPath(sendFilepath), FileAttributes.Normal);
回答by Moulde
this may help....sorry its VB not C but hey...
这可能会有所帮助....抱歉它的 VB 不是 C 但嘿...
This works
这有效
Dim fs As FileStream = Nothing
fs = File.Create("H:\test.txt")
fs.Close()
File.Delete("H:\test.txt")
This doesn't, give "file being used by another process" error
这不会,给出“另一个进程正在使用的文件”错误
File.Create("H:\test.txt")
File.Delete("H:\test.txt")