System.IO.Stream
The System.IO.Stream abstract class provides a generic view of a sequence of bytes. It serves as the base class for many other stream types such as FileStream, MemoryStream, and NetworkStream.
Namespace
Assembly
System.Runtime.dll
Syntax
public abstract class Stream : IDisposable
Inheritance
Object → MarshalByRefObject → Stream
Derived Types
- FileStream
- MemoryStream
- NetworkStream
- … and many more.
Constructors
Stream is abstract and does not expose public constructors.
Properties
CanRead
Gets a value indicating whether the current stream supports reading.
CanSeek
Gets a value indicating whether the current stream supports seeking.
CanWrite
Gets a value indicating whether the current stream supports writing.
Length
Gets the length in bytes of the stream.
Position
Gets or sets the current position within the stream.
Methods
Read(Byte[] buffer, Int32 offset, Int32 count)
Reads a block of bytes from the stream and writes the data to a buffer.
public abstract int Read (byte[] buffer, int offset, int count);
Write(Byte[] buffer, Int32 offset, Int32 count)
Writes a block of bytes to the stream from a buffer.
public abstract void Write (byte[] buffer, int offset, int count);
Seek(Int64 offset, SeekOrigin origin)
Sets the position within the current stream.
public virtual long Seek (long offset, System.IO.SeekOrigin origin);
Flush()
Clears all buffers for the current stream and causes any buffered data to be written to the underlying device.
public virtual void Flush ();
CopyTo(Stream destination)
Reads the bytes from the current stream and writes them to another stream.
public virtual void CopyTo (System.IO.Stream destination);
Example
// Copy a file using Stream
using System;
using System.IO;
class StreamCopyDemo
{
static void Main()
{
using (FileStream source = new FileStream("source.txt", FileMode.Open))
using (FileStream dest = new FileStream("dest.txt", FileMode.Create))
{
source.CopyTo(dest);
}
Console.WriteLine("File copied successfully.");
}
}