C# FileStream Dispose

来源:互联网 发布:吃什么补肾 知乎 编辑:程序博客网 时间:2024/05/22 18:23

写入一个文件后,马上调用一个函数进行读取处理。

大部分情况下都OK,但是有时候会错误。

Google到stackoverflow上一篇问答里面有模糊的解释:

http://stackoverflow.com/questions/6350224/does-filestream-dispose-close-the-file-immediately


I have some code that writes a file by saving a MemoryStream to a FileStream using MemoryStream.WriteTo(). After the file is closed it is opened up again to read some metdata...

This works about 80 - 90% of the time. The other 20% I get an exception saying the file is "in use by another process".

Does FileStream.Dispose() not release resources synchronously? Is there something going on lower in Win32 land I'm not aware of? I'm not seeing anything obvious in the .Net documentation.

Is this on NTFS? Is the file local or on a remote share? I doubt that it is Dipose() not closing the HANDLE, I think you might be experiencing what the SAMBA developers called Window's "onerous open modes" (especially if a retry of 500-1000ms on the second open succeeds). 


看这个意思,这个和底层文件系统有关?



0 0
原创粉丝点击