FileStream Constructor (SafeFileHandle, FileAccess)

Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission.

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

public FileStream(
	SafeFileHandle handle,
	FileAccess access
)

Parameters

handle
Type: Microsoft.Win32.SafeHandles.SafeFileHandle
A file handle for the file that the current FileStream object will encapsulate.
access
Type: System.IO.FileAccess
A constant that sets the CanRead and CanWrite properties of the FileStream object.

ExceptionCondition
ArgumentException

access is not a field of FileAccess.

SecurityException

The caller does not have the required permission.

IOException

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

-or-

The stream has been closed.

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.

When Close is called, the handle is also closed and the file's handle count is decremented.

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.

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.

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

For a list of common I/O tasks, see Common I/O Tasks.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft