Export (0) Print
Expand All

FileStream Constructor (IntPtr, FileAccess)

.NET Framework 1.1

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

[Visual Basic]
Public Sub New( _
   ByVal handle As IntPtr, _
   ByVal access As FileAccess _
public FileStream(
   IntPtr handle,
 FileAccess access
public: FileStream(
   IntPtr handle,
 FileAccess access
public function FileStream(
   handle : IntPtr,
 access : FileAccess


A file handle for the file that the current FileStream object will encapsulate.
A FileAccess constant that gets the CanRead and CanWrite properties of the FileStream object.


Exception Type Condition
ArgumentException access is not a field of FileAccess.
SecurityException The caller does not have the required permission.
IOException An I/O error occurs, such as a disk error.
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   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.

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. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File



Rename or move a file. File.Move


Delete a file. File.Delete


Copy a file. File.Copy


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. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing 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


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

FileStream Class | FileStream Members | System.IO Namespace | FileStream Constructor Overload List | Working with I/O | Reading Text from a File | Writing Text to a File

© 2014 Microsoft