BufferedStream.Close 方法

定義

關閉資料流並釋放與目前緩衝資料流建立關聯的任何資源 (特別是通訊端和檔案控制代碼等系統資源)。Closes the stream and releases any resources (especially system resources such as sockets and file handles) associated with the current buffered stream.

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

例外狀況

嘗試關閉資料流時發生錯誤。An error occurred while trying to close the stream.

範例

這個程式碼範例是針對 BufferedStream 類別所提供的較大範例的一部分。This code example is part of a larger example provided for the BufferedStream class.

' When bufStream is closed, netStream is in turn 
' closed, which in turn shuts down the connection 
' and closes clientSocket.
Console.WriteLine(vbCrLf & "Shutting down the connection.")
bufStream.Close()
// When bufStream is closed, netStream is in turn
// closed, which in turn shuts down the connection
// and closes clientSocket.
Console.WriteLine("\nShutting down the connection.");
bufStream.Close();
// When bufStream is closed, netStream is in turn closed,
// which in turn shuts down the connection and closes
// clientSocket.
Console::WriteLine( "\nShutting down connection." );
bufStream->Close();

備註

先前寫入緩衝區的任何資料都會先複製到基礎資料來源或儲存機制,然後再關閉緩衝的資料流程。Any data previously written to the buffer is copied to the underlying data source or repository before the buffered stream is closed. 因此,在叫用 Close 之前,不需要呼叫 Flush()Therefore, it is not necessary to call Flush() before invoking Close. 在呼叫關閉之後,緩衝資料流程上的任何作業都可能引發例外狀況。Following a call to Close, any operations on the buffered stream might raise exceptions.

除非您明確地呼叫 Flush() 或關閉,否則清除資料流程不會排清其基礎編碼器。Flushing the stream will not flush its underlying encoder unless you explicitly call 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 only be encoded after the encoder receives the adjacent character or characters.

在資料流程關閉後嘗試操作它,可能會擲回 ObjectDisposedExceptionAttempting to manipulate a stream after it has been closed might throw an ObjectDisposedException.

適用於

另請參閱