FileStream在使用statment时关闭VS FileStream [英] FileStream close VS FileStream in using statment

查看:70
本文介绍了FileStream在使用statment时关闭VS FileStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们遇到的问题是,有时文件没有完全写入(所有数据),我们使用的是以下代码



we were facing a problem that sometime the file was not written completely (all the data) and we were using code below

using (FileStream fs = new FileStream(tempFileName, FileMode.Create))
                {
                    BinaryWriter writer = new BinaryWriter(fs);
                    writer.Flush();
                    writer.Write(data);
                }  





我只做了解决这个问题的事情



only thing i did to solve the situation is put

fs.Close();





在using语句中,它开始按预期工作。现在按照MSDN的说法,diposed会在内部调用close。



知道为什么会出现这种奇怪的行为吗?



inside using statement and it's started working as expected. now as per MSDN says diposed does call close internally.

any idea why this strange behavior ?

推荐答案

将您的代码更改为以下代码。



Change your code to below code.

using (FileStream fs = new FileStream(tempFileName, FileMode.Create))
{
      BinaryWriter writer = new BinaryWriter(fs);
      writer.Write(data);
      writer.Flush();
}  





写完后刷新()。



Do a Flush() after write.


这篇关于FileStream在使用statment时关闭VS FileStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆