无法使用FileStream关闭文件 [英] can't close file with FileStream

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

问题描述

FileStream obj = new FileStream(System.Convert.ToString(row.Cells["Lec_media"].Value), FileMode.Open, FileAccess.Read, FileShare.Read);



 if (Startdate2 == JCM_NOW2)
                {
                    if (checkBox1.Checked == true)
                    {

                        try
                        {


                            obj = null;
                            System.Diagnostics.Process.Start(System.Convert.ToString(row.Cells["Lec_media"].Value));

                        }
                        finally
                        {
                            if (obj != null)
                            obj.Close();

                        }
                    }

                }



            if (Enddate2 == JCM_NOW2)
            {



                if (obj != null) ((IDisposable)obj).Dispose();

                obj.Close();

               // this code does not close the file which opened above .. why?


            }

推荐答案

在流中使用不同的模式,明确关闭(我知道,文档有点令人困惑):

Use different pattern with stream, without explicit closing (I know, the documentation is a bit confusing):
using (FileStream stream = new FileStream( /* ... */ )) {
   // use stream object here
} // stream.Dispose() is automatically called here





此外,无论结算问题如何,重要的是要保证最终为每个ob调用 System.IDisposable.Dispose() ject实现此接口。这就是为什么使用使用语句是好的(不要与混淆使用 指令 !): https://msdn.microsoft.com/en-us/library/yh598w02.aspx [ ^ ]。



还有一个建议:最有可能的是,你不需要直接使用 FileStream 。只有当你有读/写流时才真正需要它,这是一种罕见的情况;通常,您的流是只读的或只写的。因此,使用这些类中的一个或多个通常要好得多:

https://msdn.microsoft.com/en-us/library/system.io.streamwriter%28v=vs.110%29.aspx [ ^ ],

https ://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx [ ^ ],

< a href =https://msdn.microsoft.com/en-us/library/system.io.binarywriter%28v=vs.110%29.aspx> https://msdn.microsoft.com/en-us /library/system.io.binarywriter%28v=vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.io.binaryreader%28v=vs.110%29 .aspx [ ^ ]。



-SA



Besides, regardless of the closing problems, it's important to guarantee that you eventually call System.IDisposable.Dispose() for each and every object implementing this interface. That's why it's good to use the using statement (not to be confused with using directive!): https://msdn.microsoft.com/en-us/library/yh598w02.aspx[^].

One more suggestion: most likely, you don't need to use FileStream directly. It is really needed only if you have read/write stream, which is a rare case; usually, your stream is read-only or write-only. Therefore, it's usually much better to use one or more of these classes:
https://msdn.microsoft.com/en-us/library/system.io.streamwriter%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.streamreader%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.binarywriter%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.binaryreader%28v=vs.110%29.aspx[^].

—SA


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

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