如何保持文件打开? [英] How to keep file opened?

查看:97
本文介绍了如何保持文件打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

foreach (string file in dirs)
{
   using(FileStream flStream = new FileStream(file, FileMode.Open, FileAccess.Write))
  {
      MessageBox.Show(file);
  }	
}



msgbox.show之后,流关闭.如何保持每个文件打开?只是,很难想象如何..
谢谢.



After msgbox.show the Stream closes. How to keep each file opened? Just, hard to imagine how..
thanks.

推荐答案

删除using块,并确保在完成使用后自行处置flStream.
Remove the using block and make sure you dispose of flStream yourself when you''re done with it.


您为什么要?如果完成操作,则关闭文件并停止使用资源.

因为FIleStream被包装在using块中,所以它将关闭,因为在离开使用范围后,将在FileStream对象上调用Dispose,该对象随后将调用close.如果要保持打开状态,请不要使用using.但是,请记住,完成处理后,需要实现一些销毁资源的机制.
Why would you want to? If your finished with it then close the file and stop using the resources.

Because the FIleStream is wrapped in a using block of course it will close since after leaving the using scope Dispose will be called on the FileStream object which will in turn call close. If you want to keep it open, then don''t use using. However, keep in mind you need to implement some mechanism to destroy resources when you are finished with them.


首先,为什么要保持每个文件打开?

其次,您只显示文件名...不需要在using语句中

第三,该文件关闭,因为您退出了using语句.将您要对FileStream进行的所有操作放在using语句中.
First, why would you want to keep each file open?

Second, you''re only showing the file name...that doesn''t need to be in the using statement

Third, the file closes because you exit the using statement. Put anything you want to do with the FileStream inside the using statement.


这篇关于如何保持文件打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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