Class Microsoft.VisualBasic.FileSystem Syntax FileOpen (fileNumber, fileName, mode[, access[,_ share[, recordLength]]])
fileNumber (required; Integer) Any valid file number of a file opened with FileOpen.
fileName (required; String) The name and path of the file to open.
mode (required; OpenMode enumeration) The file access mode. One of the following Microsoft.VisualBasic.OpenMode enumeration values: Value | Description |
---|
Append | Sequential output of data to an existing file, starting from the end of the current file contents | Binary | Reading and writing of binary data | Input | Sequential input of data from a file | Output | Sequential output of data to a file | Random | Random access of records within a file, each of a specified length |
access (optional; OpenAccess enumeration) Specifies the allowable file operations. One of the following Microsoft.VisualBasic.OpenAccess enumeration values: Value | Description |
---|
Default | Same as ReadWrite | Read | Allows reading of data from the file | ReadWrite | Allows reading of data from, or writing of data to, the file | Write | Allows writing of data to the file |
If omitted, ReadWrite is used.
share (optional; OpenShare enumeration) Indicates how the file will interact with external processes while in use by the current process. One of the following Microsoft.VisualBasic.OpenShare enumeration values: Value | Description |
---|
Default | Same as LockReadWrite | LockRead | External processes are blocked from reading the file | LockReadWrite | External processes are blocked from reading or writing the file | LockWrite | External processes are blocked from writing the file | Shared | External processed are permitted to read and write the file |
If omitted, LockReadWrite is used.
recordLength (optional; Integer) The length of each record (for Random mode), or the size of the input/output buffer (for sequential modes). This value may not exceed 32,767. If omitted, it defaults to -1, which indicates no specific record or buffer size. Description Opens or creates a file for reading or writing Usage at a Glance There are three modes of file access: sequential, binary, and random. The Input, Output, and Append access modes are sequential access modes. Sequential access is for text files consisting of individual Unicode characters and control codes. Most of the file-manipulation functions (LineInput, Print, PrintLine, and so on) apply to files opened for sequential access. Random access (used with Random mode) is used with files that have a structurefiles that consist of records, each of which is made up of the same set of fields. For instance, a record might contain name, address, and employee ID number fields. Binary access (used with Binary mode) is for files where each byte in the file is accessible independently. fileName may be either an absolute path or a relative path from the current directory. The file may reside on a local drive or a remote drive. A new file is created if the specified file does not exist when opened in Append, Binary, Output, or Random mode. The file must exist when opened in Input mode. Always use the FreeFile function to retrieve an available file number before calling the FileOpen function. You can open an already opened file using a different file number in Binary, Input, and Random modes. However, you must close a file opened using Append or Output before you can open it with a different file number. Example The following example opens a random access data file, adds two records, and then retrieves some of the written data. Option Strict Off Module GeneralCode Structure Person <VBFixedString(10)> Public Name As String Public Age As Short End Structure Public Sub ManageData( ) ' ----- Simple record management. Dim onePerson As New Person Dim fileID As Integer = FreeFile( ) ' ----- Create the file. FileOpen(fileID, "c:\data.txt", OpenMode.Random, _ OpenAccess.ReadWrite, OpenShare.Default, Len(onePerson)) ' ----- Write out two records. onePerson.Name = "Donna" onePerson.Age = 20 FilePut (fileID, onePerson, 1) onePerson.Name = "Steve" onePerson.Age = 30 FilePut(fileID, onePerson, 2) ' ----- Get the first record back. MsgBox displays: ' "Donna is 20" FileGet(fileID, onePerson, 1) MsgBox(onePerson.Name & " is " & onePerson.Age) FileClose(fileID) End Sub End Module Since random access files require a fixed record length, the <VBFixedString(10)> attribute has been included in the structure to ensure that the Name field is a constant size. Version Differences The FileOpen procedure is new to VB under .NET. It is a replacement for the VB 6 Open statement. In Visual Basic 2005, the My.Computer.FileSystem object provides more robust access to file-management features. See Also FileClose Procedure, FileGet, FileGetObject Procedures, FilePut, FilePutObject Procedures |