Export (0) Print
Expand All

FileSystem::FileOpen Method

Opens a file for input or output. The My feature gives you better productivity and performance in file I/O operations than FileOpen. For more information, see FileSystem.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public:
static void FileOpen(
	int FileNumber, 
	String^ FileName, 
	OpenMode Mode, 
	OpenAccess Access, 
	OpenShare Share, 
	int RecordLength
)

Parameters

FileNumber
Type: System::Int32

Required. Any valid file number. Use the FreeFile function to obtain the next available file number.

FileName
Type: System::String

Required. String expression that specifies a file name—may include directory or folder, and drive.

Mode
Type: Microsoft.VisualBasic::OpenMode

Required. Enumeration specifying the file mode: Append, Binary, Input, Output, or Random. For more information, see OpenMode .

Access
Type: Microsoft.VisualBasic::OpenAccess

Optional. Enumeration specifying the operations permitted on the open file: Read, Write, or ReadWrite. Defaults to ReadWrite. For more information, see OpenAccess .

Share
Type: Microsoft.VisualBasic::OpenShare

Optional. Enumeration specifying the operations not permitted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write. Defaults to Lock Read Write. For more information, see OpenShare .

RecordLength
Type: System::Int32

Optional. Number less than or equal to 32,767 (bytes). For files opened for random access, this value is the record length. For sequential files, this value is the number of characters buffered.

ExceptionCondition
ArgumentException

Invalid Access, Share, or Mode.

ArgumentException

WriteOnly file is opened for Input.

ArgumentException

ReadOnly file is opened for Output.

ArgumentException

ReadOnly file is opened for Append.

ArgumentException

Record length is negative (and not equal to -1).

IOException

FileNumber is invalid (<-1 or >255), or FileNumber is already in use.

IOException

FileName is already open, or FileName is invalid.

The FileOpen function is provided for backward compatibility and may affect performance. For non-legacy applications, the My.Computer.FileSystem object provides better performance. For more information, see File Access with Visual Basic.

You must open a file before any I/O operation can be performed on it. FileOpen allocates a buffer for I/O to the file and determines the mode of access to use with the buffer.

Security noteSecurity Note

When writing to a file, an application may have to create a file, if the file to which it is trying to write does not exist. To do so, it needs permission for the directory in which the file is to be created. However, if the file specified by FileName does exist, the application needs Write permission only to the file itself. Wherever possible, to help improve security, create the file during deployment and grant Write permission to that file only, instead of to the whole directory. To help improve security, write data to user directories instead of to the root directory or the Program Files directory.

The channel to open can be found by using the FreeFile() function.

Security noteSecurity Note

The FileOpen function requires Read access from the FileIOPermissionAccess enumeration, which may affect its execution in partial trust situations. For more information, see FileIOPermissionAccess enumeration.

This example illustrates various uses of the FileOpen function to enable input and output to a file.

The following code opens the file TestFile in Input mode.

FileOpen(1, "TESTFILE", OpenMode.Input)
' Close before reopening in another mode.
FileClose(1)

This example opens the file in Binary mode for writing operations only.

FileOpen(1, "TESTFILE", OpenMode.Binary, OpenAccess.Write)
' Close before reopening in another mode.
FileClose(1)

The following example opens the file in Random mode. The file contains records of the structure Person.

Structure Person
    <VBFixedString(30)> Dim Name As String 
    Dim ID As Integer 
End Structure 
Public Sub ExampleMethod()
    ' Count 30 for the string, plus 4 for the integer.
    FileOpen(1, "TESTFILE", OpenMode.Random, , , 34)
    ' Close before reopening in another mode.
    FileClose(1)
End Sub

This code example opens the file in Output mode; any process can read or write to file.

FileOpen(1, "TESTFILE", OpenMode.Output, OpenAccess.Default, OpenShare.Shared)
' Close before reopening in another mode.
FileClose(1)

This code example opens the file in Binary mode for reading; other processes cannot read file.

FileOpen(1, "TESTFILE", OpenMode.Binary, OpenAccess.Read,
   OpenShare.LockRead)

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft