FileStream Constructor (IntPtr, FileAccess, Boolean, Int32)
Initializes a new instance of the FileStream class for the specified file handle, with the specified read/write permission, FileStream instance ownership, and buffer size.
[Visual Basic] Public Sub New( _ ByVal handle As IntPtr, _ ByVal access As FileAccess, _ ByVal ownsHandle As Boolean, _ ByVal bufferSize As Integer _ ) [C#] public FileStream( IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize ); [C++] public: FileStream( IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize ); [JScript] public function FileStream( handle : IntPtr, access : FileAccess, ownsHandle : Boolean, bufferSize : int );
- A file handle for the file that this FileStream object will encapsulate.
- A FileAccess constant that gets the CanRead and CanWrite properties of the FileStream object.
- true if the file handle will be owned by this FileStream instance; otherwise, false.
- The size of the buffer in bytes.
|ArgumentOutOfRangeException||bufferSize is negative.|
|IOException||An I/O error occurs, such as a disk error.|
|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.|
The FileStream object is given the specified access to the file. The ownership of the handle will be as specified. If this FileStream owns the handle, a call to the Close method will also close the handle. In particular, the file's handle count is decremented. The FileStream object is given the specified buffer size.
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 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|
|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.