This documentation is archived and is not being maintained.

FileStream Constructor (IntPtr, FileAccess, Boolean)

.NET Framework 1.1

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

[Visual Basic]
Public Sub New( _
   ByVal handle As IntPtr, _
   ByVal access As FileAccess, _
   ByVal ownsHandle As Boolean _
)
[C#]
public FileStream(
   IntPtr handle,
 FileAccess access,
 bool ownsHandle
);
[C++]
public: FileStream(
   IntPtr handle,
 FileAccess access,
 bool ownsHandle
);
[JScript]
public function FileStream(
   handle : IntPtr,
 access : FileAccess,
 ownsHandle : Boolean
);

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.
ownsHandle
true if the file handle will be owned by this FileStream instance; otherwise, false.

Exceptions

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.

Remarks

The FileStream object is given the specified access to the file. The ownership of the handle will be as specified. If this process owns the handle, a call to the Close method will also close the handle and the file's handle count is decremented. The FileStream object is given the default buffer size of 8192 bytes.

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 methods other than Close after you are done using the handle.

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

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.

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

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

Requirements

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

Show: