Stream.Close Stream.Close Stream.Close Stream.Close Method

定義

關閉目前資料流和釋放與目前資料流相關聯的任何資源 (例如通訊端和檔案控制代碼)。Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. 請確定正確地處置資料流,而非呼叫這個方法。Instead of calling this method, ensure that the stream is properly disposed.

public:
 virtual void Close();
public virtual void Close ();
abstract member Close : unit -> unit
override this.Close : unit -> unit
Public Overridable Sub Close ()

備註

這個方法會呼叫Dispose,並指定true釋放所有資源。This method calls Dispose, specifying true to release all resources. 您不必特別呼叫Close方法。You do not have to specifically call the Close method. 相反地,請確認每個Stream正確處置物件。Instead, ensure that every Stream object is properly disposed. 您可以宣告Stream內的物件using區塊 (或Using封鎖在 Visual Basic 中) 以確保會處置資料流和其所有資源,或您可以明確呼叫Dispose方法。You can declare Stream objects within a using block (or Using block in Visual Basic) to ensure that the stream and all of its resources are disposed, or you can explicitly call the Dispose method.

除非您明確地呼叫的實作,排清資料流不會清除它的基礎編碼器FlushCloseFlushing the stream will not flush its underlying encoder unless you explicitly call an implementation of Flush or Close. 設定AutoFlushtrue表示資料會從排清緩衝區資料流,但並不會清除編碼器的狀態。Setting AutoFlush to true means that data will be flushed from the buffer to the stream, but the encoder state will not be flushed. 這可讓編碼器,讓它可以正確編碼字元的下一個區塊保持其狀態 (部分的字元)。This allows the encoder to keep its state (partial characters) so that it can encode the next block of characters correctly. 這個案例會影響 UTF8,UTF7,其中某些字元可以編碼的編碼器接收或多個相鄰的字元時,才。This scenario affects UTF8 and UTF7 where certain characters can be encoded only after the encoder receives the adjacent character or characters.

嘗試在關閉資料流之後,管理資料流可能會擲回ObjectDisposedExceptionAttempts to manipulate the stream after the stream has been closed might throw an ObjectDisposedException.

給繼承者的注意事項

在衍生類別中,不會覆寫Close()方法,而是將所有Stream中的清除邏輯Dispose(Boolean)方法。In derived classes, do not override the Close() method, instead, put all of the Stream cleanup logic in the Dispose(Boolean) method. 如需詳細資訊,請參閱 <<c0> 實作 Dispose 方法For more information, see Implementing a Dispose Method.

適用於

另請參閱