Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

FileStream Constructor (String, FileMode, FileSystemRights, FileShare, Int32, FileOptions)

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

Initializes a new instance of the FileStream class with the specified path, creation mode, access rights and sharing permission, the buffer size, and additional file options.

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

'Declaration
Public Sub New ( _
	path As String, _
	mode As FileMode, _
	rights As FileSystemRights, _
	share As FileShare, _
	bufferSize As Integer, _
	options As FileOptions _
)
'Usage
Dim path As String
Dim mode As FileMode
Dim rights As FileSystemRights
Dim share As FileShare
Dim bufferSize As Integer
Dim options As FileOptions

Dim instance As New FileStream(path, mode, rights, share, bufferSize, options)
public FileStream (
	String path, 
	FileMode mode, 
	FileSystemRights rights, 
	FileShare share, 
	int bufferSize, 
	FileOptions options
)
public function FileStream (
	path : String, 
	mode : FileMode, 
	rights : FileSystemRights, 
	share : FileShare, 
	bufferSize : int, 
	options : FileOptions
)

Parameters

path

A relative or absolute path for the file that the current FileStream object will encapsulate.

mode

A FileMode constant that determines how to open or create the file.

rights

A FileSystemRights constant that determines the access rights to use when creating access and audit rules for the file.

share

A FileShare constant that determines how the file will be shared by processes.

bufferSize

A positive Int32 value greater than 0 indicating the buffer size. For bufferSize values between zero and eight, the actual buffer size is set to eight bytes.

options

A FileOptions constant that specifies additional file options.

Exception typeCondition

ArgumentNullException

path is a null reference (Nothing in Visual Basic).

ArgumentException

path is an empty string (""), contains only white space, or contains one or more invalid characters.

-or-

path refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in an NTFS environment.

NotSupportedException

path refers to a non-file device, such as "con:", "com1:", "lpt1:", etc. in a non-NTFS environment.

ArgumentException

path is an empty string (""), contains only white space, or contains one or more invalid characters.

ArgumentOutOfRangeException

bufferSize is negative or zero.

-or-

mode, access, or share contain an invalid value.

FileNotFoundException

The file cannot be found, such as when mode is FileMode.Truncate or FileMode.Open, and the file specified by path does not exist. The file must already exist in these modes.

PlatformNotSupportedException

The current operating system is not Windows NT or later.

IOException

An I/O error occurs, such as specifying FileMode.CreateNew and the file specified by path already exists.

-or-

The stream has been closed.

SecurityException

The caller does not have the required permission.

DirectoryNotFoundException

The specified path is invalid, such as being on an unmapped drive.

UnauthorizedAccessException

The access requested is not permitted by the operating system for the specified path, such as when access is Write or ReadWrite and the file or directory is set for read-only access.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

The .NET Framework does not support direct access to physical disks through paths that are device names, such as "\\.\PHYSICALDRIVE0 ".

Use this FileStream constructor to apply access rights at the point of creation of a file. To access or modify rights on an existing file, consider using the GetAccessControl and SetAccessControl methods.

The fileOptions parameter is used to provide access to more advanced operations that can be leveraged when creating a FileStream object.

The path parameter can be a file name, including a file on a Universal Naming Convention (UNC) share.

NoteNote

path is not required to be a file stored on disk; it can be any part of a system that supports access through streams. For example, depending on the system, this class can access a physical device.

CanSeek is true for all FileStream objects that encapsulate files. If path indicates a device that does not support seeking, the CanSeek property on the resulting FileStream is false. For additional information, see CanSeek.

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.