StreamWriter.Close 方法

定义

关闭当前 StreamWriter 对象和基础流。

public:
 override void Close();
public override void Close();
override this.Close : unit -> unit
Public Overrides Sub Close ()

例外

当前编码不支持显示 Unicode 代理项对的一半。

示例

下面的代码示例演示了该方法 Close

// close the file by closing the writer
sw.Close();
' Close the file by closing the writer.
Sw.Close()

注解

此方法重写 Stream.Close

此实现调用CloseDispose传递true值的方法。

必须调用 Close 以确保所有数据正确写出到基础流。 调用 Close后,对异常的任何操作 StreamWriter 都可能会引发异常。 如果磁盘上没有足够的空间,调用 Close 将引发异常。

除非显式调用 FlushClose调用流,否则刷新流不会刷新其基础编码器。 AutoFlush设置为true表示数据将从缓冲区刷新到流,但不会刷新编码器状态。 这使编码器能够保持其状态(部分字符),以便它可以正确编码下一个字符块。 此方案影响 UTF8 和 UTF7,其中某些字符只能在编码器接收相邻字符或字符后进行编码。

适用于

另请参阅