VB.NET Language in a Nutshell

   
FileOpen Procedure

Class

Microsoft.VisualBasic.FileSystem

Syntax

FileOpen( filenumber, filename, mode, access, share, recordlength )

filenumber (required; Integer)

An available file number.

filename (required; String)

The name of the file to open , along with an optional path .

mode (optional; OpenMode enum)

The file-access mode. Options are: OpenMode.Append, OpenMode. Binary, OpenMode.Input, OpenMode.Output, or OpenMode.Random (the default value).

access (optional; OpenAccess enum)

Specifies the allowable operations by the current process. Options are: OpenAccess.Default , OpenAccess.Read , OpenAccess.ReadWrite (the default value), or OpenAccess.Write .

share (optional; OpenShare enum)

Specifies the allowable operations by other processes. Options are: OpenShare.Shared (the default value), OpenShare.LockRead , OpenShare. LockWrite , or OpenShare.LockreadWrite .

recordlength (optional; Integer (at most, 32767)

The length of the record (for random access) or of the I/O buffer (for sequential access).

Description

Opens a disk file for reading and/or writing

Rules at a Glance

Lock type

Description

Shared

Other processes can open the file for both read and write operations.

LockRead

Other processes can only write to the file.

LockWrite

Other processes can only read from the file.

LockReadWrite

Other processes cannot open the file.

Open mode

Meaning of Len=

Random

Length in bytes of each record

Binary

Ignored

Append/Input/Output

The number of characters to buffer

Example

The following example opens a random access data file, adds two records, and then retrieves the second record:

Module modMain Structure Person <vbFixedString(10)> Public Name As String Public Age As Short End Structure Public Sub Main Dim APerson As New Person( ) Dim fr As Integer = FreeFile( ) FileOpen(fr, "c:\data.txt", OpenMode.Random, _ OpenAccess.ReadWrite, OpenShare.Default, len(APerson)) APerson.Name = "Donna" APerson.Age = 20 FilePut(fr, APerson, 1) APerson.Name = "Steve" APerson.Age = 30 FilePut(fr, APerson, 2) FileGet(fr, APerson, 2) MsgBox(APerson.Age) FileClose(fr) End Sub End Module

Since random access files require a fixed record length, note the use of the <vbFixedString( length )> attribute to ensure that the Name field is a constant size .

Programming Tips and Gotchas

VB.NET/VB 6 Differences

The FileOpen procedure is new to VB.NET. It is a more or less direct replacement for the VB 6 Open statement.

See Also

FileClose Procedure, FileGet, FileGetObject Procedures, FilePut, FilePutObject Procedures

   

Категории