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,其中某些字元只能在編碼器收到相鄰字元或字元之後進行編碼。

適用於

另請參閱