Stream.Close Stream.Close Stream.Close Stream.Close Method

定义

关闭当前流并释放与之关联的所有资源(如套接字和文件句柄)。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.

如果不显式调用的实现,刷新该流不会刷新其基础编码器FlushCloseFlushing the stream will not flush its underlying encoder unless you explicitly call an implementation of Flush or Close. 设置AutoFlushtrue意味着数据从缓冲区刷新到流中,但编码器状态不会进行刷新。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.

适用于

另请参阅