Export (0) Print
Expand All

FileStream.FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions) Constructor

Initializes a new instance of the FileStream class with the specified path, creation mode, read/write and sharing permission, the access other FileStreams can have to the same file, 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, _
	access As FileAccess, _
	share As FileShare, _
	bufferSize As Integer, _
	options As FileOptions _
)
'Usage
Dim path As String
Dim mode As FileMode
Dim access As FileAccess
Dim share As FileShare
Dim bufferSize As Integer
Dim options As FileOptions

Dim instance As New FileStream(path, mode, access, share, bufferSize, options)
public FileStream (
	String path, 
	FileMode mode, 
	FileAccess access, 
	FileShare share, 
	int bufferSize, 
	FileOptions options
)
public function FileStream (
	path : String, 
	mode : FileMode, 
	access : FileAccess, 
	share : FileShare, 
	bufferSize : int, 
	options : FileOptions
)
Not applicable.

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.

access

A FileAccess constant that determines how the file can be accessed by the FileStream object. This gets the CanRead and CanWrite properties of the FileStream object. CanSeek is true if path specifies a disk 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 one and eight, the actual buffer size is set to eight bytes.

options

A FileOptions value 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.

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.

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.

-or-

Encrypted is specified for options, but file encryption is not supported on the current platform.

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

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.

For a list of common I/O tasks, see Common I/O Tasks.

The following example writes data to a file and then reads the data using the FileStream object.

Imports System
Imports System.IO
Imports System.Text
Imports System.Security.AccessControl



Module FileStreamExample

    Sub Main()
        Try
            ' Create a file and write data to it.
            ' Create an array of bytes.
            Dim messageByte As Byte() = Encoding.ASCII.GetBytes("Here is some data.")

            ' Create a file using the FileStream class.
            Dim fWrite As New FileStream("test.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 8, FileOptions.None)

            ' Write the number of bytes to the file.
            fWrite.WriteByte(System.Convert.ToByte(messageByte.Length))

            ' Write the bytes to the file.
            fWrite.Write(messageByte, 0, messageByte.Length)

            ' Close the stream.
            fWrite.Close()


            ' Open a file and read the number of bytes.
            Dim fRead As New FileStream("test.txt", FileMode.Open)

            ' The first byte is the string length.
            Dim length As Integer = Fix(fRead.ReadByte())

            ' Create a new byte array for the data.
            Dim readBytes(length) As Byte

            ' Read the data from the file.
            fRead.Read(readBytes, 0, readBytes.Length)

            ' Close the stream.
            fRead.Close()

            ' Display the data.
            Console.WriteLine(Encoding.ASCII.GetString(readBytes))

            Console.WriteLine("Done writing and reading data.")
        Catch e As Exception
            Console.WriteLine(e)
        End Try

        Console.ReadLine()

    End Sub
End Module


Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft