FileStream Constructor (IntPtr, FileAccess)
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 _ ) [C#] public FileStream( IntPtr handle, FileAccess access ); [C++] public: FileStream( IntPtr handle, FileAccess access ); [JScript] 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.
|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:
- FileIOPermission for reading, writing, and appending to files. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write, and FileIOPermissionAccess.Append.