FileStream.Dispose 是否立即关闭文件?
我有一些代码,通过使用 MemoryStream.WriteTo() 将 MemoryStream 保存到 FileStream 来写入文件。文件关闭后,它会再次打开以读取一些元数据...
这在大约 80 - 90% 的情况下有效。另外 20% 我收到一个异常,说该文件“正在被另一个进程使用”。
FileStream.Dispose() 不同步释放资源吗?在 Win32 领域是否发生了一些我不知道的事情?我在 .Net 文档中没有看到任何明显的内容。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽可能“立即”。由于
未完成的写入、更新目录信息的延迟等原因,很容易出现一些延迟。也可能是防病毒软件检查您更改的文件。这可能是调用
Thread.Sleep(1)
的罕见情况。但为了完全安全,您必须捕获(任何)异常并重试一定次数。As "immediately" as possible. There can easily be some lag due to
outstanding writes, delay in updating the directory info etc. It could also be anti-virus software checking your changed file.This may be a rare case where a
Thread.Sleep(1)
is called for. But to be totally safe you will have to catch the (any) exception and try again a set number of times.