Opens a file for input or output.
The My feature gives you greater productivity and performance in file I/O operations than FileOpen. For more information, see My.Computer.FileSystem Object.
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 _ )
Invalid Access, Share, or Mode.
WriteOnly file is opened for Input.
ReadOnly file is opened for Output.
ReadOnly file is opened for Append.
Record length is negative (and not equal to -1).
FileNumber is invalid (<-1 or >255), or FileNumber is already in use.
FileName is already open, or FileName is invalid.
See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.
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.
When writing to a file, 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 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, rather than to the entire directory. To help improve security, write data to user directories rather than to the root directory or the Program Files directory.
The channel to open can be found using the FreeFile() function.
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.
This example opens the file in Binary mode for writing operations only.
The following example opens the file in Random mode. The file contains records of the structure Person.
This code example opens the file in Output mode; any process can read or write to file.
This code example opens the file in Binary mode for reading; other processes cannot read file.
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)