StringWriter.Close 方法

定义

关闭当前的 StringWriter 和基础流。

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

示例

此代码示例是为构造函数提供的更大示例的 StringWriter(StringBuilder) 一部分。

strWriter->Close();

// Since the StringWriter is closed, an exception will 
// be thrown if the Write method is called. However, 
// the StringBuilder can still manipulate the string.
strBuilder->Insert( 0, "Invalid " );
Console::WriteLine( strWriter->ToString() );
strWriter.Close();

// Since the StringWriter is closed, an exception will
// be thrown if the Write method is called. However,
// the StringBuilder can still manipulate the string.
strBuilder.Insert(0, "Invalid ");
Console.WriteLine(strWriter.ToString());
strWriter.Close()

' Since the StringWriter is closed, an exception will 
' be thrown if the Write method is called. However, 
' the StringBuilder can still manipulate the string.
strBuilder.Insert(0, "Invalid ")
Console.WriteLine(strWriter.ToString())

注解

有关创建文件和向文件写入文本的示例,请参阅 如何:将文本写入文件。 有关从文件读取文本的示例,请参阅 如何:从文件读取文本。 有关从二进制文件读取和写入的示例,请参阅 如何:读取和写入新创建的数据文件

此方法重写 Stream.Close

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

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

适用于

另请参阅