Export (0) Print
Expand All

FileStream Constructor (SafeFileHandle, FileAccess, Int32, Boolean)

Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, buffer size, and synchronous or asynchronous state.

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

public FileStream(
	SafeFileHandle handle,
	FileAccess access,
	int bufferSize,
	bool isAsync
)

Parameters

handle
Type: Microsoft.Win32.SafeHandles.SafeFileHandle

A file handle for the file that this FileStream object will encapsulate.

access
Type: System.IO.FileAccess

A constant that sets the CanRead and CanWrite properties of the FileStream object.

bufferSize
Type: System.Int32

A positive Int32 value greater than 0 indicating the buffer size. The default buffer size is 4096.

isAsync
Type: System.Boolean

true if the handle was opened asynchronously (that is, in overlapped I/O mode); otherwise, false.

ExceptionCondition
ArgumentException

The handle parameter is an invalid handle.

-or-

The handle parameter is a synchronous handle and it was used asynchronously.

ArgumentOutOfRangeException

The bufferSize parameter is negative.

IOException

An I/O error, such as a disk error, occurred.

-or-

The stream has been closed.

SecurityException

The caller does not have the required permission.

UnauthorizedAccessException

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

You set the isAsync parameter to true to open the file handle asynchronously. When the parameter is true, the stream utilizes overlapped I/O to perform file operations asynchronously. However, the parameter does not have to be true to call the ReadAsync, WriteAsync, or CopyToAsync method. When the isAsync parameter is false and you call the asynchronous read and write operations, the UI thread is still not blocked, but the actual I/O operation is performed synchronously.

FileStream assumes that it has exclusive control over the handle. Reading, writing, or seeking while a FileStream is also holding a handle could result in data corruption. For data safety, call Flush before using the handle, and avoid calling any methods other than Close after you are done using the handle. Alternately, read and write to the handle before calling this FileStream constructor.

FileShare.Read is the default for those FileStream constructors without a FileShare parameter.

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.

For a list of common file and directory operations, see Common I/O Tasks.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft