我知道这可能看起来很愚蠢,但为什么以下代码只有在我关闭()文件时才有效?如果我不关闭文件,则不会写入整个流.
脚步:
文件对象超出范围时,是否不应自动刷新或关闭?我是C#的新手,但我习惯在C++析构函数中添加对Close()的调用.
// 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();
Run Code Online (Sandbox Code Playgroud)
Ben*_*igt 22
C#没有自动确定性清理.如果要控制运行时,必须确保调用清理功能.该using
块是这样做的最常见的方式.
如果你没有自己进行清理调用,那么当垃圾收集器决定其他东西需要内存时会发生清理,这可能需要很长时间.
using (StreamWriter outfile = new StreamWriter("../../file.txt")) {
outfile.Write(b64img);
} // everything is ok, the using block calls Dispose which closes the file
Run Code Online (Sandbox Code Playgroud)
编辑:正如哈维指出的那样,当对象被收集时将尝试进行清理,但这并不能保证成功.为了避免循环引用的问题,运行时不会尝试以"正确"的顺序完成对象,因此FileStream
在StreamWriter
终结器运行并尝试刷新缓冲输出时实际上已经死了.
如果处理需要清理的对象,请使用using
(对于本地作用域)或通过调用IDisposable.Dispose
(对于长期对象,例如类成员的指示对象)进行显式处理.
归档时间: |
|
查看次数: |
8577 次 |
最近记录: |