A Visual Basic application can take three basic approaches to file system manipulation: Visual Basic methods, System.IO Framework classes, and the My.Computer.FileSystem namespace. This appendix summaries the properties, methods, and events provided by these approaches.
The following table summarizes the Visual Basic methods for working with files. They let a program create, open, read, write, and learn about files.
Function | Purpose |
---|---|
EOF | Returns True if the file is at the end of file. |
FileClose | Closes an open file. |
FileGet | Reads data from a file opened in Random and Binary mode into a variable. |
FileGetObject | Reads data as an object from a file opened in Random and Binary mode into a variable. |
FileOpen | Opens a file for reading or writing. Parameters indicate the mode (Append, Binary, Input, Output, or Random), access type (Read, Write, or ReadWrite), and sharing (Shared, LockRead, LockWrite, or LockReadWrite). |
FilePut | Writes data from a variable into a file opened for Random or Binary access. |
FilePutObject | Writes an object from a variable into a file opened for Random or Binary access. |
FreeFile | Returns a file number that is not currently associated with any file in this application. You should use FreeFile to get file numbers rather than using arbitrary numbers such as 1. |
Input | Reads data written into a file by the Write method back into a variable. |
InputString | Reads a specific number of characters from the file. |
LineInput | Returns the next line of text from the file. |
Loc | Returns the current position within the file. |
LOF | Returns the file’s length in bytes. |
| Prints values into the file. Multiple values separated by commas are aligned at tab boundaries. |
PrintLine | Prints values followed by a new line into the file. Multiple values separated by commas are aligned at tab boundaries. |
Seek | Moves to the indicated position within the file. |
Write | Writes values into the file, delimited appropriately so that they can later be read by the Input method. |
WriteLine | Writes values followed by a new line into the file, delimited appropriately so that they can later be read by the Input method. |
The following table describes Visual Basic methods that manipulate directories and files. They let an application list, rename, move, copy, and delete files and directories.
Method | Purpose |
---|---|
ChDir | Changes the application’s current working directory. |
ChDrive | Changes the application’s current working drive. |
CurDir | Returns the application’s current working directory. |
Dir | Returns a file matching a directory path specification that may include wildcards, and matching certain file properties such as ReadOnly, Hidden, or Directory. The first call to Dir should include a path. Subsequent calls can omit the path to fetch the next matching file for the initial path. Dir returns file names without the path and returns Nothing when no more files match. |
FileCopy | Copies a file to a new location. |
FileDateTime | Returns the date and time when the file was created or last modified. |
FileLen | Returns the length of a file in bytes. |
GetAttr | Returns a value indicating the file’s attributes. The value is a combination of the values vbNormal, vbReadOjnly, vbHidden, vbSystem, vbDirectory, vbArchive, and vbAlias. |
Kill | Permanently deletes a file. |
MkDir | Creates a new directory. |
Rename | Renames a directory or file. |
RmDir | Deletes an empty directory. |
SetAttr | Sets the file’s attributes The value is a combination of the values vbNormal, vbReadOjnly, vbHidden, vbSystem, vbDirectory, vbArchive, and vbAlias. |