Stream.Flush 方法

定義

當在衍生類別中覆寫時,會清除這個資料流的所有緩衝區,並造成所有緩衝資料都寫入基礎裝置。When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.

public:
 abstract void Flush();
public abstract void Flush ();
abstract member Flush : unit -> unit
Public MustOverride Sub Flush ()

例外狀況

發生 I/O 錯誤。An I/O error occurs.

備註

Flush在執行緩衝區的資料流程上覆寫。Override Flush on streams that implement a buffer. 使用這個方法可將任何資訊從基礎緩衝區移至其目的地、清除緩衝區或兩者。Use this method to move any information from an underlying buffer to its destination, clear the buffer, or both. 根據物件的狀態,您可能必須修改資料流程中目前的位置 (例如,如果基礎資料流程支援搜尋) 。Depending upon the state of the object, you might have to modify the current position within the stream (for example, if the underlying stream supports seeking). 如需詳細資訊,請參閱 CanSeekFor additional information see CanSeek.

在衍生自 Stream 但不支援寫入的類別中, Flush 通常會將實作為空白方法,以確保與其他類型的完整相容性, Stream 因為這是將唯讀資料流程排清的有效方式。In a class derived from Stream that doesn't support writing, Flush is typically implemented as an empty method to ensure full compatibility with other Stream types since it's valid to flush a read-only stream.

使用 StreamWriter 或類別時 BinaryWriter ,請勿清除基底 Stream 物件。When using the StreamWriter or BinaryWriter class, do not flush the base Stream object. 相反地,請使用類別的 FlushClose 方法,以確保先將資料排清到基礎資料流程,然後再寫入至檔案。Instead, use the class's Flush or Close method, which makes sure that the data is flushed to the underlying stream first and then written to the file.

適用於

另請參閱