为什么我必须关闭()在C#中的文件? [英] Why must I Close() a file in C#?

查看:151
本文介绍了为什么我必须关闭()在C#中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能看起来很可笑,但为什么以下code只有当我关闭()的文件?如果我不关闭文件,整个流不写。

步骤:

  1. 运行在窗体加载此code。
  2. 使用鼠标
  3. 关闭表一旦被显示出来。
  4. 程序将终止。

如果没有文件对象被刷新或自动关闭时,它超出范围?我是新的C#,但我已经习惯了将调用关闭()在C ++中析构函数。

  //注意:完成产量约为87KB。如果没有关闭(),它缺少约2KB底。

//转换成PNG,然后将其转换成一个base64连接codeD字符串。
字符串b64img = ImageToBase64(IMG,ImageFormat.Png);
// Base64编码的图像保存到更多的测试和外部认证的文本文件。
StreamWriter的OUTFILE =新的StreamWriter(../../ file.txt的);
outfile.Write(b64img);
//如果不关闭文件时,Windows不会写这一切到磁盘。不知道为什么
//这将是。
outfile.Close();
 

解决方案

C#不具有自动确定清理。你必须确保调用清理功能,如果你想在运行时控制。该使用块是这样做的最常见的方式。

如果你不把在清理调用自己,那么将清理时,垃圾收集器决定的内存要别的东西,这可能是一个很长的时间之后发生的。

 使用(StreamWriter的OUTFILE =新的StreamWriter(../../ file.txt的)){
    outfile.Write(b64img);
} //一切正常,使用块调用Dispose它关闭文件
 

编辑:哈维指出的,而当对象被收集在清理将尝试,这不是任何成功的保证。为了避免循环引用问题,运行时不会尝试在正确为了完成目标,因此的FileStream 实际上已经死了的时候的StreamWriter 终结器运行,并试图刷新缓冲的输出。

如果您在使用(局部范围的使用),或致电 IDisposable的需要清理,做到明确,无论是与对象处理。处置(长期存活的对象,如类成员的所指对象)。

I know this might seem silly, but why does the following code only work if I Close() the file? If I don't close the file, the entire stream is not written.

Steps:

  1. Run this code on form load.
  2. Close form using mouse once it is displayed.
  3. Program terminates.

Shouldn't the file object be flushed or closed automatically when it goes out of scope? I'm new to C#, but I'm used to adding calls to Close() in C++ destructors.

// Notes: complete output is about 87KB. Without Close(), it's missing about 2KB at the end.

// Convert to png and then convert that into a base64 encoded string.
string b64img = ImageToBase64(img, ImageFormat.Png);
// Save the base64 image to a text file for more testing and external validation.
StreamWriter outfile = new StreamWriter("../../file.txt");
outfile.Write(b64img);
// If we don't close the file, windows will not write it all to disk. No idea why
// that would be.
outfile.Close();

解决方案

C# doesn't have automatic deterministic cleanup. You have to be sure to call the cleanup function if you want to control when it runs. The using block is the most common way of doing this.

If you don't put in the cleanup call yourself, then cleanup will happen when the garbage collector decides the memory is needed for something else, which could be a very long time later.

using (StreamWriter outfile = new StreamWriter("../../file.txt")) {
    outfile.Write(b64img);
} // everything is ok, the using block calls Dispose which closes the file

EDIT: As Harvey points out, while the cleanup will be attempted when the object gets collected, this isn't any guarantee of success. To avoid issues with circular references, the runtime makes no attempt to finalize objects in the "right" order, so the FileStream can actually already be dead by the time the StreamWriter finalizer runs and tries to flush buffered output.

If you deal in objects that need cleanup, do it explicitly, either with using (for locally-scoped usage) or by calling IDisposable.Dispose (for long-lived objects such as referents of class members).

这篇关于为什么我必须关闭()在C#中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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