简体   繁体   English

尝试获取FileStream,关闭阅读器有多重要?

[英]FileStream in a try and catch, how critical is it to close the reader?

I want to make sure to close the reader after creating a reader when there is an exception. 我想确保在出现异常时创建读取器后关闭读取器。 So I want to include a finally bracket to this try and catch. 因此,我想在此尝试和抓住中加入最后一个括号。 But I can't close it in the finally bracket unless I have the reader declared outside above the try bracket. 但是除非我在try括号之外声明了读者,否则我无法在final括号中关闭它。 But if I do that I won't be catching any exceptions when creating the reader. 但是,如果这样做的话,在创建阅读器时,我不会捕获任何异常。 I am not sure how critical it is to close the reader or not. 我不确定关闭读者与关闭读者有多重要。

XmlSerializer xmlSr = new XmlSerializer(typeof(List<ProjectObject>));
try
{
    FileStream reader = new FileStream(mTextFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
    List<ProjectObject> addProjects = (List<ProjectObject>)xmlSr.Deserialize(reader);
    mSharedDriveLocalProjects = addProjects;
    reader.Close();
}
catch
{
    MessageBox.Show("Failed to load XML file");
}

The above is my code. 以上是我的代码。 Is it better to have it this way? 用这种方式更好吗?

XmlSerializer xmlSr = new XmlSerializer(typeof(List<ProjectObject>));
FileStream reader = new FileStream(mTextFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
try
{
    List<ProjectObject> addProjects = (List<ProjectObject>)xmlSr.Deserialize(reader);
    mSharedDriveLocalProjects = addProjects;
}
catch
{
    MessageBox.Show("Failed to load XML file");
}
finally
{
    reader.Close();
}

The Using Statement is here to solve these dilemmas Using语句在这里解决这些难题

 XmlSerializer xmlSr = new XmlSerializer(typeof(List<ProjectObject>));
 using(FileStream reader = new FileStream(mTextFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
 {
     try
     {
         List<ProjectObject> addProjects = (List<ProjectObject>)xmlSr.Deserialize(reader);
         mSharedDriveLocalProjects = addProjects;
     }
     catch
     {
         MessageBox.Show("Failed to load XML file");
     }
}

Any disposable object declared and created in a using statement is disposed at the exit of the block (the dispose method of the object is called and this method usually takes care to clean up everything, including the closure of the file) 在using语句中声明和创建的所有一次性对象都放置在块的出口(调用对象的dispose方法,该方法通常会注意清理所有内容,包括关闭文件)

Note that a using statement is like a try/finally, so if you want to catch the exception and show a nice error message to your user or log it somewhere, you still need to explicitly prepare a try/catch block around your code. 请注意,using语句类似于try / finally,因此,如果您想捕获异常并向用户显示错误消息或将其记录在某处,则仍需要在代码周围显式准备try / catch块。

C# and .NET library provide a nice idiomatic way of doing this with using keyword: C#和.NET库提供了一种很好的惯用方式,可以using关键字:

using (var reader = new FileStream(mTextFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) {
    try {
        ...
    } catch {
        ...
    }
}

Since FileStream is IDisposable , and because its Dispose() method calls Close() , wrapping reader into using block provides a convenient method of ensuring that the stream is closed after you are done with it. 由于FileStreamIDisposable ,并且因为它的Dispose()方法调用Close() ,所以将reader包装到using块中提供了一种方便的方法,以确保在完成流后将其关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM