StreamWriter.Close 方法

定義

關閉目前的 StreamWriter 物件和基礎資料流。

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

例外狀況

目前的編碼不支援顯示 Unicode Surrogate 字組的其中一半。

範例

下列程式代碼範例示範 Close 方法。

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

備註

這個方法會覆寫 Stream.Close

這個實作會 Close 呼叫 Dispose 傳遞 true 值的方法。

您必須呼叫 Close ,以確保所有資料都已正確寫出到基礎數據流。 在呼叫 Close之後,上 StreamWriter 的任何作業可能會引發例外狀況。 如果磁碟空間不足,呼叫 Close 將會引發例外狀況。

除非您明確呼叫 FlushClose,否則清除數據流不會排清其基礎編碼器。 設定 AutoFlushtrue 表示數據會從緩衝區排清到數據流,但不會排清編碼器狀態。 這可讓編碼器保持其狀態 (部分字元) ,以便正確地編碼下一個字元區塊。 此案例會影響UTF8和UTF7,其中某些字元只能在編碼器收到相鄰字元或字元之後進行編碼。

適用於

另請參閱