CHAPTER 20 FILE I/O AND OBJECT SERIALIZATION
Format: FAT32
Label: My Passport
At this point, you have investigated some core behaviors of the Directory, DirectoryInfo, and
DriveInfo classes. Next, you’ll learn how to create, open, close, and destroy the files that populate a
given directory.
Source Code You can find the DriveInfoApp project under the Chapter 20 subdirectory.
Working with the FileInfo Class
As shown in the previous DirectoryApp example, the FileInfo class allows you to obtain details
regarding existing files on your hard drive (e.g., time created, size, and file attributes) and aids in the
creation, copying, moving, and destruction of files. In addition to the set of functionality inherited by
FileSystemInfo, you can find some core members unique to the FileInfo class, which you can see
described in Table 20-4.
Table 20-4. FileInfo Core Members
762
Member
Meaning in Life
AppendText()
Creates a StreamWriter object (described later) that appends text to a file.
CopyTo()
Copies an existing file to a new file.
Create()
Creates a new file and returns a FileStream object (described later) to interact
with the newly created file.
CreateText()
Creates a StreamWriter object that writes a new text file.
Delete()
Deletes the file to which a FileInfo instance is bound.
Directory
Gets an instance of the parent directory.
DirectoryName
Gets the full path to the parent directory.
Length
Gets the size of the current file.
MoveTo()
Moves a specified file to a new location, providing the option to specify a new
file name.
Name
Gets the name of the file.