C# FileStream.close() 不会为其他进程释放文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14955770/
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
FileStream.close() does not free file for other processes
提问by Vogel612
I have Following Code in a Page_Load called function. When the Page is loaded the first time after starting Visual Studio, everything works out fine.
But any other opening call to the File after that returns IOException: "File is in use by another process"
, even when directly opening the File in VisualStudio Solution this Error is returned(of course not as Exception)
我在 Page_Load 调用函数中有以下代码。启动 Visual Studio 后第一次加载页面时,一切正常。
但是在此之后对文件的任何其他打开调用都会返回IOException: "File is in use by another process"
,即使在 VisualStudio 解决方案中直接打开文件时也会返回此错误(当然不是异常)
FileStream mailinglist_FileStream = new FileStream(@"\foobarFile.txt", FileMode.Open);
PeekingStreamReader mailinglist_Reader = new PeekingStreamReader(mailinglist_FileStream);
//Do some stuff with the file
mailinglist_FileStream.Close();
mailinglist_Reader.Close();
mailinglist_Reader.Dispose();
mailinglist_FileStream.Dispose();
Why is the file still locked? and why does fully restarting Visual Studio reset the File? when checking file-Properties it says:
为什么文件仍然被锁定?为什么完全重新启动 Visual Studio 会重置文件?检查文件属性时,它说:
Build Action: Content
Copy to output directory: do not Copy
构建操作:内容
复制到输出目录:不要复制
I am only reading this File. can i do something similiar to adLockOptimistic
, so that multiple processes can access the File?
我只是在读这个文件。我可以做类似的事情adLockOptimistic
,以便多个进程可以访问文件吗?
采纳答案by Matías Fidemraizer
Why is the file still locked? and why does fully restarting Visual Studio reset the File? when checking file-Properties it says [...] I don't know why the file is still locked: probably because your code fails before the stream is closed/disposed.
为什么文件仍然被锁定?为什么完全重新启动 Visual Studio 会重置文件?检查文件属性时,它说 [...] 我不知道为什么文件仍然被锁定:可能是因为您的代码在关闭/处置流之前失败。
About "why fully restarting Visual Studio [...]": because you may be using IIS Express or ASP.NET Dev Server whose are closed when you close the IDE, so locks on files are released since the process holding the locks is no longer running.
关于“为什么完全重新启动 Visual Studio [...]”:因为您可能正在使用 IIS Express 或 ASP.NET Dev Server,它们在您关闭 IDE 时已关闭,因此文件锁定被释放,因为持有锁定的进程没有运行时间更长。
And about "why is the file still locked?[...]" it could be because the file stream isn't closed because sometimes the thread may not end successfully and the locks aren't released.
关于“为什么文件仍然被锁定?[...]”这可能是因为文件流没有关闭,因为有时线程可能没有成功结束并且没有释放锁。
As other answer said, check how using
block may avoid that IDisposable
objects wouldn't be disposed:
正如其他答案所说,检查using
块如何避免IDisposable
对象不会被处理:
// FileShare.ReadWrite will allow other processes
// to read and write the target file even if other processes
// are working with the same file
using (FileStream mailinglist_FileStream = new FileStream(@"\foobarFile.txt", FileMode.Open, FileShare.ReadWrite))
using (PeekingStreamReader mailinglist_Reader = new PeekingStreamReader(mailinglist_FileStream))
{
// Do your stuff. Using blocks will call Dispose() for
// you even if something goes wrong, as it's equal to a try/finally!
// Also check how using statements can be chained without extra { }
}
I am only reading this File. can i do something similiar to adLockOptimistic, so that multiple processes can access the File?
我只是在读这个文件。我可以做一些类似于 adLockOptimistic 的事情,以便多个进程可以访问文件吗?
Yes, take a look at File.Open
method and FileShare
enumeration:
是的,看看File.Open
方法和FileShare
枚举:
回答by Adam K Dean
Try using using
blocks, it may not fix your lock problem, but it is better form for disposable objects.
尝试使用using
块,它可能无法解决您的锁定问题,但它是一次性物品的更好形式。
using (FileStream mailinglist_FileStream = new FileStream(@"\foobarFile.txt", FileMode.Open))
{
using (PeekingStreamReader mailinglist_Reader = new PeekingStreamReader(mailinglist_FileStream))
{
...
}
}
Also, try closing mailinglist_Reader
before mailinglist_FileStream
.
另外,尝试mailinglist_Reader
在 之前关闭mailinglist_FileStream
。
回答by ????
Learn to use using
:
学习使用using
:
using (FileStream fileStream = File.Open(@"C:\somefile", FileMode.Open, FileAccess.Read))
{
...
}
The using
construct ensures that the file will be closed when you leave the block even if an exception is thrown.
using
即使抛出异常,该构造也可确保在您离开块时关闭文件。
Your problem might not be here, but somewhere else in your code. You'll have to go through all your code and look for places where you have opened files but not put it inside a using
statement.
您的问题可能不在这里,而是在您的代码中的其他地方。您必须检查所有代码并查找已打开文件但未将其放入using
语句的位置。