FileOpen Procedure

   
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

  • 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 designed 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 is designed to be used with files that have a structure more specifically , files 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 social security number fields. The binary access mode is for binary access, where each byte in the file is accessible independently.

  • filename may include the directory or folder and drive; if these are omitted, the file is assumed to reside in the current working directory. If filename does include drive and path information, this may take the form of a path relative to the local system or a UNC path.

  • The default mode for opening a disk file (when mode is not specified) is OpenMode.Random .

  • If the specified file does not exist when opening in Input mode, an error occurs.

  • A new file is created if the specified file does not exist when opening in Append, Binary, Output, or Random mode.

  • access allows you to restrict the actions that can be taken against the file in the current process, by specifying Read , Write , or ReadWrite . The default is OpenAccess.ReadWrite .

  • The share argument allows you to restrict the operations performed on the open file by other processes, and accepts one of the following members of the OpenShare enumeration:

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.

  • The recordlength argument is treated differently, depending upon the open mode, as the following table shows:

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

  • To avoid using the file number of an already open file and generating an error, use the FreeFile function to allocate the next available file number.

  • 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.

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

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net