Table of Contents

Class FileStream

Namespace
System.IO
Assembly
System.IO.FileSystem.dll

Provides a Stream for a file, supporting both synchronous and asynchronous read and write operations.

public class FileStream : Stream, IDisposable
Inheritance
FileStream
Implements
Inherited Members
Extension Methods

Constructors

FileStream(string, FileMode)

Initializes a new instance of the FileStream class with the specified path and creation mode.

public FileStream(string path, FileMode mode)

Parameters

path string

A relative or absolute path for the file that the current FileStream object will encapsulate.

mode FileMode

One of the enumeration values that determines how to open or create the file.

FileStream(string, FileMode, FileAccess)

Initializes a new instance of the FileStream class with the specified path, creation mode, and read/write permission.

public FileStream(string path, FileMode mode, FileAccess access)

Parameters

path string

A relative or absolute path for the file that the current FileStream object will encapsulate.

mode FileMode

One of the enumeration values that determines how to open or create the file.

access FileAccess

A bitwise combination of the enumeration values that determines how the file can be accessed by the FileStream object. This also determines the values returned by the CanRead and CanWrite properties of the FileStream object.

Properties

CanRead

Gets a value that indicates whether the current stream supports reading.

public override bool CanRead { get; }

Property Value

bool

CanSeek

Gets a value that indicates whether the current stream supports seeking.

public override bool CanSeek { get; }

Property Value

bool

CanWrite

Gets a value that indicates whether the current stream supports writing.

public override bool CanWrite { get; }

Property Value

bool

FilePath

Gets the full file-system path of the current file, if the file has a path.

public string FilePath { get; }

Property Value

string

Length

Gets the length in bytes of the stream.

public override long Length { get; }

Property Value

long

Name

Gets the name of the file including the file name extension.

public string Name { get; }

Property Value

string

The name of the file including the file name extension.

Position

Gets or sets the current position of this stream.

public override long Position { get; set; }

Property Value

long

Methods

Close()

Closes the current stream and releases any resources associated with the current stream.

public override void Close()

Dispose(bool)

Releases the unmanaged resources used by the FileStream and optionally releases the managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Finalize()

Destructor

protected override void Finalize()

Flush()

Clears buffers for this stream and causes any buffered data to be written to the file.

public override void Flush()

Read(byte[], int, int)

Reads a block of bytes from the stream and writes the data in a given buffer.

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

Parameters

buffer byte[]

When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

offset int

The byte offset in array at which the read bytes will be placed.

count int

The maximum number of bytes to read.

Returns

int

The total number of bytes read into the buffer. This might be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached.

Read(SpanByte)

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public override int Read(SpanByte buffer)

Parameters

buffer SpanByte

A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source.

Returns

int

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, or zero (0) if the end of the stream has been reached.

ReadByte()

Reads a byte from the file and advances the read position one byte.

public override int ReadByte()

Returns

int

Seek(long, SeekOrigin)

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

public override long Seek(long offset, SeekOrigin origin)

Parameters

offset long

The point relative to origin from which to begin seeking.

origin SeekOrigin

Specifies the beginning, the end, or the current position as a reference point for offset, using a value of type SeekOrigin.

Returns

long

The new position in the stream.

SetLength(long)

Sets the length of this stream to the given value.

public override void SetLength(long value)

Parameters

value long

The new length of the stream.

Write(byte[], int, int)

Writes a block of bytes to the file stream.

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

Parameters

buffer byte[]

The buffer containing data to write to the stream.

offset int

The zero-based byte offset in array from which to begin copying bytes to the stream.

count int

The maximum number of bytes to write.

WriteByte(byte)

Writes a byte to the current position in the file stream.

public override void WriteByte(byte value)

Parameters

value byte

A byte to write to the stream.