This documentation is archived and is not being maintained.

BufferedStream.Close Method

.NET Framework 1.1

Closes the stream and releases any resources (especially system resources such as sockets and file handles) associated with the current buffered stream.

[Visual Basic]
Overrides Public Sub Close()
public override void Close();
public: void Close();
public override function Close();


Exception Type Condition
IOException An error occurred while trying to close the stream.


Any data previously written to the buffer is copied to the underlying data source or repository before the buffered stream is closed. 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.

Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close. 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. This scenario affects UTF8 and UTF7 where certain characters can only be encoded after the encoder receives the adjacent character or characters.

Attempting to manipulate a stream after it has been closed might throw an ObjectDisposedException.


[Visual Basic, C#, C++] This code example is part of a larger example provided for the BufferedStream class.

[Visual Basic] 
' 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.")

// Create a NetworkStream that owns clientSocket and 
// then create a BufferedStream on top of the NetworkStream.
// Both streams are disposed when execution exits the
// using statement.
    netStream = new NetworkStream(clientSocket, true),
    bufStream = 
          new BufferedStream(netStream, streamBufferSize))
    // Check whether the underlying stream supports seeking.
    Console.WriteLine("NetworkStream {0} seeking.\n",
        bufStream.CanSeek ? "supports" : "does not support");

    // Send and receive data.
        SendData(netStream, bufStream);
        ReceiveData(netStream, bufStream);

    // When execution exits the using statement, netStream
    // is disposed, which in turn shuts down the connection 
    // and closes clientSocket.
    Console.WriteLine("\nShutting down the connection.");

// When bufStream is closed, netStream is in turn closed,
// which in turn shuts down the connection and closes
// clientSocket.
Console::WriteLine(S"\nShutting down connection.");

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

BufferedStream Class | BufferedStream Members | System.IO Namespace | Flush | Working with I/O | Reading Text from a File | Writing Text to a File