Traditional Visual Basic File Management


Visual Basic has included significant file management features since its first release. In fact, there are more features in Visual Basic that deal with file and directory manipulation than with pretty much anything else.

Most of the functions that allow you to read and modify file content use a file handle, a numeric identifier that refers to a specific open file. This file handle is generated with the FreeFile function, and must be obtained before calling any of the traditional Visual Basic file features.

Dim fileID As Integer fileID = FreeFile() FileOpen(fileID, "C:\TestData.txt", OpenMode.Append) PrintLine(fileID, "Important output to file.") FileClose(fileID) 


File handle-based file manipulation works just fine, but it is so early-'90s. It's not really a .NET technology, and is not object-based at all (unless you consider that an Integer is an object). Therefore, we won't be covering it in this book, or using it in the Library Project. Table 15-1 lists the major Visual Basic features that use file handles. If you need to know about the handle-based features in Visual Basic, or if your work involves migrating pre-.NET Visual Basic applications, use this table to help you locate full feature details in the technical documentation supplied with Visual Basic.

Table 15-1. Visual Basic Features That Use File Handles

Feature

Description

EOF

Returns a Boolean indicating whether the current position in the file is at or past the end of the file. Use this function to determine when to stop reading existing data from a file.

FileAttr

Indicates the file attributes currently set on an open file handle.

FileClose

Closes a specific file opened using a file handle.

FileGet

Retrieves structured data from a file and stores it in a matching object.

FileGetObject

Same as FileGet, but with slightly different data typing support.

FileOpen

Opens a file for input or output.

FilePut

Writes an object to a file in a structured manner.

FilePutObject

Same as FilePut, but with slightly different data typing support.

FileWidth

Sets the default line width for formatted text output files.

FreeFile

Returns the next available file handle.

Input

Retrieves a value previously written to a file using Write or WriteLine.

InputString

Retrieves a specific number of characters from an input file.

LineInput

Returns a complete line of input from a file.

Loc

Returns the current byte or record location in the file.

Lock

Locks a file or specific records in a file so that others cannot make changes.

LOF

Returns the length of an open file, in bytes.

Print

Sends text output to a file.

PrintLine

Sends text output to a file, ending it with a line terminator.

Reset

Closes all files currently opened with file handles.

Seek

Gets or sets the current position in a file.

SPC

This function helps format text for output to columnar text files.

TAB

This function helps format text for output to columnar text files.

Unlock

Removes locks previously set with Lock.

Write

Writes data to a file using a consistent format that can be easily read later.

WriteLine

Same as Write, but ends the output with a line terminator.





Start-to-Finish Visual Basic 2005. Learn Visual Basic 2005 as You Design and Develop a Complete Application
Start-to-Finish Visual Basic 2005: Learn Visual Basic 2005 as You Design and Develop a Complete Application
ISBN: 0321398009
EAN: 2147483647
Year: 2006
Pages: 247
Authors: Tim Patrick

Similar book on Amazon

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