Class FileStream
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
stringA relative or absolute path for the file that the current FileStream object will encapsulate.
mode
FileModeOne 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
stringA relative or absolute path for the file that the current FileStream object will encapsulate.
mode
FileModeOne of the enumeration values that determines how to open or create the file.
access
FileAccessA 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
stringA relative or absolute path for the file that the current FileStream object will encapsulate.
mode
FileModeOne of the enumeration values that determines how to open or create the file.
access
FileAccessA 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
FileShareA 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
stringA relative or absolute path for the file that the current FileStream object will encapsulate.
mode
FileModeOne of the enumeration values that determines how to open or create the file.
access
FileAccessA 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
FileShareA bitwise combination of the enumeration values that determines how the file will be shared by processes.
bufferSize
intA 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
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
booltrue 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
intThe byte offset in array at which the read bytes will be placed.
count
intThe 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
SpanByteThe 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
Seek(long, SeekOrigin)
Sets the current position of this stream to the given value.
public override long Seek(long offset, SeekOrigin origin)
Parameters
offset
longThe point relative to origin from which to begin seeking.
origin
SeekOriginSpecifies 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
longThe 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
intThe zero-based byte offset in array from which to begin copying bytes to the stream.
count
intThe 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
byteA byte to write to the stream.