Stream Class

Namespace: System.IO

Summary

Represents a base class for all streams. This is an abstract class.

Remarks

A stream is a sequence of bytes, and a direct successor to the stream can be any device that has bytes associated with it—input, output, or both. For example, a file stream is an Stream to a file on the disk. A network stream is a Stream over the network. Memory-backed streams are also Streams. The Stream class and its derived classes provide the core functionality for working with streams of data.

The Stream class is an abstract class that provides a generic view of a sequence of bytes. You cannot instantiate the Stream class directly. Instead, create an instance of a derived class such as MemoryStream, FileStream, or NetworkStream.

Members

Properties

CanRead

Gets a value indicating whether the current stream supports reading.

public virtual bool CanRead { get; }

CanSeek

Gets a value indicating whether the current stream supports seeking.

public virtual bool CanSeek { get; }

CanWrite

Gets a value indicating whether the current stream supports writing.

public virtual bool CanWrite { get; }

Length

Gets the length in bytes, in bytes.

public virtual long Length { get; }

Position

Gets or sets the current position within the stream.

public virtual long Position { get; set; }

Methods

BeginRead

Asynchronously reads the bytes from the current stream and writes them to a buffer.

public virtual IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)

Parameters:

Returns: An IAsyncResult that represents the asynchronous read, which could still be pending.

BeginWrite

Asynchronously writes bytes to the current stream from a buffer.

public virtual IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)

Parameters:

Close

Releases the unmanaged resources and closes the stream.

public virtual void Close()

CopyTo

Copies the current stream to another stream.

public virtual void CopyTo(Stream destination)

Parameters:

Dispose

Releases all resources used by the current instance of the Stream class.

public void Dispose()

EndRead

Confirms that an asynchronous read operation has completed and returns the number of bytes read.

public virtual int EndRead(IAsyncResult asyncResult)

Parameters:

Returns: The number of bytes read from the stream.

EndWrite

Confirms that an asynchronous write operation has completed.

public virtual void EndWrite(IAsyncResult asyncResult)

Parameters:

Flush

Clears all buffers for this stream and causes any buffered data to be written to the underlying device.

public virtual void Flush()

Seek

Sets the current position of this stream to the given value.

public virtual long Seek(long offset, SeekOrigin origin)

Parameters:

Returns: The new position within the current stream.

SetLength

Sets the length of this stream to the given value.

public virtual void SetLength(long value)

Parameters:

Read

When overridden in a derived class, reads a sequence of bytes from the current stream into an array of bytes, starting at the specified offset in the array and for the specified number of bytes.

public abstract int Read(byte[] buffer, int offset, int count)

Parameters:

Returns: The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available.

Write

When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream according to the number of bytes written.

public abstract void Write(byte[] buffer, int offset, int count)

Parameters: