Stream.Flush Stream.Flush Stream.Flush Stream.Flush Method


当在派生类中重写时,将清除该流的所有缓冲区,并使得所有缓冲数据被写入到基础设备。When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.

 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.

使用时StreamWriterBinaryWriter类中,不会刷新基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.