Share via


FileStream Class

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations.

Inheritance Hierarchy

System. . :: . .Object
  System. . :: . .MarshalByRefObject
    System.IO. . :: . .Stream
      System.IO..::..FileStream

Namespace:  System.IO
Assembly:  System.IO (in System.IO.dll)

Syntax

'Declaration
Public Class FileStream _
    Inherits Stream
public class FileStream : Stream
public ref class FileStream : public Stream
type FileStream =  
    class
        inherit Stream
    end
public class FileStream extends Stream

The FileStream type exposes the following members.

Constructors

  Name Description
Public method FileStream(String, FileMode) Initializes a new instance of the FileStream class with the specified path and creation mode.
Public method FileStream(String, FileMode, FileAccess) Initializes a new instance of the FileStream class with the specified path, creation mode, and read/write permission.
Public method 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 method FileStream(String, FileMode, FileAccess, FileShare, Int32) Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, and buffer size.

Top

Properties

  Name Description
Public property CanRead Gets a value indicating whether the current stream supports reading. (Overrides Stream. . :: . .CanRead.)
Public property CanSeek Gets a value indicating whether the current stream supports seeking. (Overrides Stream. . :: . .CanSeek.)
Public property CanTimeout Gets a value that determines whether the current stream can time out. (Inherited from Stream.)
Public property CanWrite Gets a value indicating whether the current stream supports writing. (Overrides Stream. . :: . .CanWrite.)
Public property IsAsync Gets a value indicating whether the FileStream was opened asynchronously or synchronously.
Public property Length Gets the length in bytes of the stream. (Overrides Stream. . :: . .Length.)
Public property Name Gets the name of the FileStream that was passed to the constructor.
Public property Position Gets or sets the current position of this stream. (Overrides Stream. . :: . .Position.)
Public property ReadTimeout Gets or sets a value, in milliseconds, that determines how long the stream will attempt to read before timing out. (Inherited from Stream.)
Public property WriteTimeout Gets or sets a value, in miliseconds, that determines how long the stream will attempt to write before timing out. (Inherited from Stream.)

Top

Methods

  Name Description
Public method Close Closes the current stream and releases any resources (such as sockets and file handles) associated with the current stream. Instead of calling this method, ensure that the stream is properly disposed. (Inherited from Stream.)
Public method Dispose() () () () Releases all resources used by the Stream. (Inherited from Stream.)
Protected method Dispose(Boolean) Releases the unmanaged resources used by the FileStream and optionally releases the managed resources. (Overrides Stream. . :: . .Dispose(Boolean).)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Ensures that resources are freed and other cleanup operations are performed when the garbage collector reclaims the FileStream. (Overrides Stream. . :: . .Finalize() () () ().)
Public method Flush Clears buffers for this stream and causes any buffered data to be written to the file. (Overrides Stream. . :: . .Flush() () () ().)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method Read Reads a block of bytes from the stream and writes the data in a given buffer. (Overrides Stream. . :: . .Read(array<Byte> [] () [] [], Int32, Int32).)
Public method ReadByte Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream. (Inherited from Stream.)
Public method Seek Sets the current position of this stream to the given value. (Overrides Stream. . :: . .Seek(Int64, SeekOrigin).)
Public method SetLength Sets the length of this stream to the given value. (Overrides Stream. . :: . .SetLength(Int64).)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Public method Write Writes a block of bytes to the file stream. (Overrides Stream. . :: . .Write(array<Byte> [] () [] [], Int32, Int32).)
Public method WriteByte Writes a byte to the current position in the stream and advances the position within the stream by one byte. (Inherited from Stream.)

Top

Remarks

Use the FileStream class to read from, write to, open, and close files on a file system, and to manipulate other file-related operating system handles, including pipes, standard input, and standard output. FileStream buffers input and output for better performance.

The IsAsync property detects whether the file handle was opened asynchronously. You specify this value when you create an instance of the FileStream class using a constructor that has an isAsync, useAsync, or options parameter. When the property is true, the stream utilizes overlapped I/O to perform file operations asynchronously.

The Seek method supports random access to files. Seek allows the read/write position to be moved to any position within the file. This is done with byte offset reference point parameters. The byte offset is relative to the seek reference point, which can be the beginning, the current position, or the end of the underlying file, as represented by the three members of the SeekOrigin enumeration.

If a process terminates with part of a file locked or closes a file that has outstanding locks, the behavior is undefined.

For directory operations and other file operations, see the File, Directory, and Path classes. The File class is a utility class that has static methods primarily for the creation of FileStream objects based on file paths. The MemoryStream class creates a stream from a byte array and is similar to the FileStream class.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.IO Namespace