Export (0) Print
Expand All

FileStream Constructor (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.

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

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


Type: System.String

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

Type: System.IO.FileMode

A constant that determines how to open or create the file.

Type: System.IO.FileAccess

A constant that determines how the file can be accessed by the FileStream object. This gets the CanRead and CanWrite properties of the FileStream object. CanSeek is true if path specifies a disk file.

Type: System.IO.FileShare

A constant that determines how the file will be shared by processes.

Type: System.Int32

A positive Int32 value greater than 0 indicating the buffer size. For bufferSize values between one and eight, the actual buffer size is set to eight bytes.


path is null.


path is an empty string (""), contains only white space, or contains one or more invalid characters.


path refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in an NTFS environment.


path refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in a non-NTFS environment.




bufferSize is negative or zero.


mode, access, or share contain an invalid value.


The file cannot be found, such as when mode is FileMode.Truncate or FileMode.Open, and the file specified by path does not exist. The file must already exist in these modes.


An I/O error occurs, such as specifying FileMode.CreateNew and the file specified by path already exists.


The system is running Windows 98 or Windows 98 Second Edition and share is set to FileShare.Delete.


The stream has been closed.


The caller does not have the required permission.


The specified path is invalid, such as being on an unmapped drive.


The access requested is not permitted by the operating system for the specified path, such as when access is Write or ReadWrite and the file or directory is set for read-only access.



The .NET Framework does not support direct access to physical disks through paths that are device names, such as "\\.\PHYSICALDRIVE0 ".

The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.


path is not required to be a file stored on disk; it can be any part of a system that supports access through streams. For example, depending on the system, this class can access a physical device.

CanSeek is true for all FileStream objects that encapsulate files. If path indicates a device that does not support seeking, the CanSeek property on the resulting FileStream is false. For additional information, see CanSeek.

Caution noteCaution:

When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.




.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

© 2014 Microsoft