Stream.Close 方法

定義

關閉目前資料流和釋放與目前資料流相關聯的任何資源 (例如通訊端和檔案控制代碼)。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.

除非您明確地呼叫或的執行,否則清除資料流程不會排清其基礎編碼器 Flush CloseFlushing the stream will not flush its underlying encoder unless you explicitly call an implementation of Flush or Close. 如果設定 AutoFlush 為, true 表示資料將會從緩衝區排清到資料流程,但不會清除編碼器狀態。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. 如需詳細資訊,請參閱 執行 Dispose 方法For more information, see Implementing a Dispose Method.

適用於

另請參閱