Export (0) Print
Expand All

FileStream Constructor (SafeFileHandle, FileAccess, Int32)

Note: This constructor is new in the .NET Framework version 2.0.

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

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

public FileStream (
	SafeFileHandle handle,
	FileAccess access,
	int bufferSize
)
public FileStream (
	SafeFileHandle handle, 
	FileAccess access, 
	int bufferSize
)
public function FileStream (
	handle : SafeFileHandle, 
	access : FileAccess, 
	bufferSize : int
)

Parameters

handle

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

access

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

bufferSize

A positive Int32 value greater than 0 indicating the buffer size.

Exception typeCondition

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 occurs, such as a disk error.

-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.

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.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Append text to a file.

How to: Open and Append to a Log File

File.AppendText

FileInfo.AppendText

Rename or move a file.

File.Move

FileInfo.MoveTo

Delete a file.

File.Delete

FileInfo.Delete

Copy a file.

File.Copy

FileInfo.CopyTo

Get the size of a file.

FileInfo.Length

Get the attributes of a file.

File.GetAttributes

Set the attributes of a file.

File.SetAttributes

Determine if a file exists.

File.Exists

Read from a binary file.

How to: Read and Write to a Newly Created Data File

Write to a binary file.

How to: Read and Write to a Newly Created Data File

Retrieve a file extension.

Path.GetExtension

Retrieve the fully qualified path of a file.

Path.GetFullPath

Retrieve the file name and extension from a path.

Path.GetFileName

Change the extension of a file.

Path.ChangeExtension

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft