妥善处置文件流和二进制流和文件流处置 [英] Proper disposal of filestreams and binary streams and disposing of filestreams

查看:152
本文介绍了妥善处置文件流和二进制流和文件流处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正因为如此,我试着防错我的代码,并最终使它看起来相当混乱。

As it is, I tried error-proofing my code and ended up making it look quite messy.

我已经设置了阅读某一类型文件的功能。我希望函数返回false,如果有问题,或者真要是一切正常。我无法搞清楚如何组织的一切。

I have a function set up to read a certain type of file. I want the function to return false if there was a problem, or true if everything worked. I'm having trouble figuring out how to structure everything.

我有试图打开一个文件流的初始try-catch块。 ,虽然以后,我有一定的偏移某些其他检查在阅读过程中,我做出这样的文件大小和值。我设置它的方式是,如果与else语句。如:

I have an initial try-catch block that tries to open a file stream. After that though, I have certain other checks I make during the reading process such as file size and values at certain offsets. The way I set it up was with if else statements. Such as:

if(condition){

}
else{
    MessageBox.Show("There was an error");
    br.Dispose();
    fs.Dispose();
    return false;
}



... BR是二进制阅读器和FS的文件流。有很多这样的块,好像不好的做法来写同样的事情,这么多次。我所想到的第一件事就是来包装整个事情在一个try-catch语句和投掷,而不是使用的if else块例外。我记得读过有关的try-catch语句,它的好,有他们,而不是与他们包装的一切。说实话,我还是不完全理解为什么它会是不好的做法来包装的try catch语句的一切,因为他们只有当有一个错误,在这种情况下,程序会南反正效果...

...br being the binary reader and fs the filestream. There are many blocks like this, and it seems like bad practice to write the same thing so many times. The first thing that comes to mind is to wrap the entire thing in a try-catch statement and throw exceptions instead of using the if else blocks. I remember when reading about try-catch statements that it's good to have them, but not to wrap everything with them. To be honest, I still don't completely understand why it would be bad practice to wrap everything in try catch statements, as they only have an effect when there's an error, in which case the program is going south anyway...

另外,我必须关闭二进制阅读器和文件流,或将关闭一个接近其他?有什么办法使用它们,而无需处置它们?

Also, do I have to close the binary reader and file stream, or will closing one close the other? Is there any way to use them without having to dispose of them?

推荐答案

如何利用的使用关键字?这包您一试使用的的IDisposable - finally块;

How about making use of the using keyword? this wraps your use of an IDisposable in a try - finally block;

bool success = true;

using(var fs = new FileStream(fileName, FileMode.Create)))
using(var br = new BinaryReader(fs))
{
  // do something
  success = result;
}

return success;



使用块将确保双方FILESTREAM和二进制读者总是正确关闭和处置嵌套。

The nested using blocks will ensure that both the filestream and binary reader are always properly closed and disposed.

您可以阅读更多有关使用MSDN 。它使得使用的IDisposable 有点整洁,消除了明确的错误时抛出处理的需要。

You can read more about using in MSDN. It makes the use of IDisposable a little neater, removing the need for explicit excpetion handling.

关于你的发言:

我记得读过有关的try-catch语句,它是很好的
有他们,而不是与他们包装的一切。

I remember when reading about try-catch statements that it's good to have them, but not to wrap everything with them.

我总是用简单的规则,如果我不能处理,并从异常的特定代码块内恢复,不要尝试抓住它。允许例外冒泡堆栈到一个点,它更有意义,抓住它。通过这种方法,你会发现,你并不需要添加很多的try-catch块,你会趋向于一体化的服务点使用它们(如文件系统,网络等),但你的业务逻辑几乎总是免费的异常处理机制。

I always use the simple rule that if I cannot handle and recover from an exception within a particular block of code, don't try to catch it. Allow the exception to 'bubble up' the stack to a point where it makes more sense to catch it. With this approach you will find that you do not need to add many try-catch blocks, you will tend to use them at the point of integration with services (such as filesystem, network etc ...) but your business logic is almost always free of exception handling mechanisms.

这篇关于妥善处置文件流和二进制流和文件流处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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