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. 调用 Close 后,缓冲流上的任何操作都可能引发异常。Following a call to Close, any operations on the buffered stream might raise exceptions.

除非显式调用 Flush() 或 Close,否则刷新流将不会刷新其基础编码器。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.

适用于

另请参阅