|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98
Chapter 17 File Handling And File Controls
- If you need an immediate solution to:
- Using The Common Dialogs File Open And File Save As
- Creating A File
- Getting A Files Length
- Opening A File
- Writing To A Sequential File
- Writing To A Random Access File
- Writing To A Binary File
- Reading From Sequential Files
- Reading From Random Access Files
- Reading From Binary Files
- Accessing Any Record In A Random Access File
- Closing A File
- Saving Files From Rich Text Boxes
- Opening Files In Rich Text Boxes
- Saving Files From Picture Boxes
- Opening Files In Picture Boxes
- Using The Drive List Box Control
- Using The Directory List Box Control
- Using The File List Box Control
- Creating And Deleting Directories
- Changing Directories
- Copying A File
- Moving A File
- Deleting A File
- When Was A File Created? Last Modified? Last Accessed?
- Creating A TextStream
- Opening A TextStream
- Writing To A TextStream
- Reading From A TextStream
- Closing A TextStream
In Depth
This chapter focuses on file handling and using the file controls in Visual Basic. Here, well see how to:
- Use the Common Dialogs File Open and File Save As (you can find more information on this topic in Chapter 11).
- Create a file
- Open a file
- Read from a file
- Write to a file
- Close a file
- Read and write files with rich text boxes
- Use the file controls like the directory list box and drive list box
- Determine a files creation date, last modified date, and more
- Move and copy files
- Use the TextStream object
There are three main ways to access files in Visual Basic: as sequential files, as random access files, and as binary files (you set the way youll treat a file when you open it). Well get an overview of these types of files before turning to the Immediate Solutions.
Sequential Access Files
Sequential files are like tape cassettesyou read data from them in a sequential manner. If you want data at the end of the file, you have to read all the intervening data first. Sequential files are often organized into text strings in Visual Basic. Here are the Visual Basic statements and functions you use with sequential files (the # symbol refers to an open file, as well see):
- Open
- Line Input #
- Print #
- Write #
- Input$
- Input #
- Close
In addition, Visual Basic supports TextStream objects to make working with sequential files easier, as well see later in this chapter. Here are the major TextStream methods:
- Read
- ReadAll
- ReadLine
- Write
- WriteBlankLines
- WriteLine
- Close
When do you use sequential files? If youve got a text file full of variable-length strings, you usually treat that file as sequential. You can also use sequential files to store binary-format items like numbers.
Random Access Files
If sequential files are like cassettes, random access files are more like CDs. Random files are organized into records (usually of the same length), and you can read a particular record without having to read all the intervening datayou can move to that record in a file directly, just as you can move to a CD track.
Here are the Visual Basic statements and functions you use with random access files:
- Type
End Type (to create and format records)
- Open
- Put #
- Len
- Seek
- LOC
- Get #
- Close
When do you use random access files? If you want to create your own database files, formatted as you want them, youd organize them into records. In fact, any file that you want to organize into records is best formatted as a random access file.
Binary Files
Binary files are simply unformatted binary data, and Visual Basic does not interpret (such as looking for text strings) or organize the contents (into records) of such files at all. These files are just bytes to Visual Basic, and the statements and functions you usually use with these files include the following:
- Open
- Get
- Put
- Seek
- Close
Binary files include EXE files, graphics files, and so on.
The FileSystemObject
Besides the preceding file types, Visual Basic includes the FileSystemObject for easy file manipulation on disk. This object includes a number of methods for copying, moving, and deleting files such as these:
- GetFile
- CopyFile
- DeleteFile
- MoveFile
- FileExists
- CreateFolder
- CreateTextFile
- OpenTextFile
In fact, you use the FileSystemObject to create TextStream objects (with methods like CreateTextFile and OpenTextFile). Well see more about this topic later in this chapter.
Thats it for the overview of files and file handling. Its time to turn to the Immediate Solutions.
Immediate Solutions
Using The Common Dialogs File Open And File Save As
The usual way to start working with files is to get a file name from the user using the Common Dialogs File Open or File Save As. Weve covered these dialogs in depth in Chapter 11, but well provide a quick overview here.
You display the File Open and File Save As dialog boxes with the Common Dialog controls ShowOpen and ShowSave methods. These methods need no arguments passed to themto set various options, you set the Common Dialog controls Flags property (see Chapter 11). You can also set the Filter property so the dialog box displays only certain types of files, such as text files.
|