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

FileOpen Function

Opens a file for input or output.

Public Sub FileOpen( _
   ByVal FileNumber As Integer, _
   ByVal FileName As String, _
   ByVal Mode As OpenMode, _
   Optional ByVal Access As OpenAccess = OpenAccess.Default, _
   Optional ByVal Share As OpenShare = OpenShare.Default, _
   Optional ByVal RecordLength As Integer = -1 _
)

Parameters

FileNumber
Required. Any valid file number. Use the FreeFile function to obtain the next available file number.
FileName
Required. String expression that specifies a file name — may include directory or folder, and drive.
Mode
Required. Enum specifying the file mode: Append, Binary, Input, Output, or Random.
Access
Optional. Keyword specifying the operations permitted on the open file: Read, Write, or ReadWrite. Defaults to ReadWrite.
Share
Optional. Enum specifying the operations restricted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write. Defaults to Lock Read Write.
RecordLength
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.

Exceptions/Errors

Exception type Error number Condition
ArgumentException 5 Invalid Access, Share, or Mode.
ArgumentException 5 WriteOnly file is opened for Input.
ArgumentException 5 ReadOnly file is opened for Output.
ArgumentException 5 ReadOnly file is opened for Append.
ArgumentException 5 Record length is negative (and not equal to -1).
IOException 52 FileNumber is invalid (<-1 or >255), or FileNumber is already in use.
IOException 55 FileName is already open, or FileName is invalid.

Remarks

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 Note   When writing to files, an application may need 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 only needs Write permission to the file itself. Wherever possible, it is more secure to create the file during deployment and only grant Write permission to that file, rather than to the entire directory. It is also more secure to write data to user directories than to the root directory or the Program Files directory.

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

Security 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 and Requesting Permissions.

Example

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
' Count 30 for the string, plus 4 for the integer.
FileOpen(1, "TESTFILE", OpenMode.Random, , , 34)
' Close before reopening in another mode.
FileClose(1)

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

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

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

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

Smart Device Developer Notes

This function is not supported.

Requirements

Namespace: Microsoft.VisualBasic

Module: FileSystem

Assembly: Microsoft Visual Basic .NET Runtime (in Microsoft.VisualBasic.dll)

See Also

FileClose Function | FreeFile Function | File Access with Visual Basic Run-Time Functions | ArgumentException Class | IOException Class

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.