FreeFile Function


FreeFile Function

Class

Microsoft.VisualBasic.FileSystem

Syntax

     Dim result As Integer = FreeFile(  ) 

Description

The FreeFile function returns the next available file number for use with the FileOpen procedure.

Usage at a Glance

  • It is good programming practice to always use FreeFile to obtain a file number for use with the FileOpen procedure, even for a given file number previously used with another file that is now closed.

  • The number returned by FreeFile always represents the next available unopened file number. After retrieving this file number, you should immediately call the FileOpen procedure, particularly if your file access code resides in a multithreaded application or component. Failure to do so may cause the same handle to be assigned to two different variables, so that one of the calls to FileOpen fails.

Example

This example shows that file numbers should be used immediately. The following code is correct.

     Dim fileNum1 As Integer     Dim fileNum2 As Integer     fileNum1 = FreeFile(  )     FileOpen(fileNum1, "c:\file1.txt", OpenMode.Input)     fileNum2 = FreeFile(  )     FileOpen(fileNum2, "c:\file2.txt", OpenMode.Input) 

The following code, however, is incorrect, since fileNum1 and fileNum2 will likely be assigned the same file number.

     Dim fileNum1 As Integer     Dim fileNum2 As Integer     fileNum1 = FreeFile(  )     fileNum2 = FreeFile(  )     FileOpen(fileNum1, "c:\file1.txt", OpenMode.Input)     ' ----- The next line will generate an error.     FileOpen(fileNum2, "c:\file2.txt", OpenMode.Input) 

Version Differences

In Visual Basic 2005, the My.Computer.FileSystem object provides more robust access to file management features.

See Also

FileOpen Procedure




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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