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. CanSeek is true if path specifies a disk file.

FileStream(string, FileMode, FileAccess, FileShare)

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

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

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. CanSeek is true if path specifies a disk file.

share FileShare

A bitwise combination of the enumeration values that determines how the file will be shared by processes.

FileStream(string, FileMode, FileAccess, FileShare, int)

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

public FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize)

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. CanSeek is true if path specifies a disk file.

share FileShare

A bitwise combination of the enumeration values that determines how the file will be shared by processes.

bufferSize int

A positive int value greater than 0 indicating the buffer size. The default buffer size is 2048.

Properties

CanRead

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

public override bool CanRead { get; }

Property Value

bool

true if the stream supports reading; otherwise, false.

CanSeek

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

public override bool CanSeek { get; }

Property Value

bool

true if the stream supports seeking; false if the stream is closed or if the FileStream was constructed from an operating-system handle such as a pipe or output to the console.

CanWrite

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

public override bool CanWrite { get; }

Property Value

bool

true if the stream supports writing; false if the stream is closed or was opened with read-only access.

Length

Gets the length in bytes of the stream.

public override long Length { get; }

Property Value

long

The length in bytes of the stream.

Name

Gets the absolute path of the file opened in the FileStream.

public string Name { get; }

Property Value

string

A string that is the absolute path of the file.

Position

Gets or sets the current position of this stream.

public override long Position { get; set; }

Property Value

long

The current position of this stream.

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()

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)

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

The buffer to write the data into.

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.

Exceptions

NotImplementedException

This method is currently not implemented.

ReadByte()

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

public override int ReadByte()

Returns

int

The byte, cast to an int, or -1 if the end of the stream has been reached.

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.