Using File System Classes
The techniques described in Lesson 29 let you read and write text ﬁles. They also demonstrate
techniques that you’ll ﬁnd useful when you deal with streams other than text ﬁles.
Those techniques don’t let you manipulate the ﬁle system itself, however. They let you read or
write a ﬁle, but they don’t let you rename a ﬁle, move a ﬁle to a different directory, or delete a ﬁle.
This lesson describes ﬁle system classes that make these and other common ﬁle manipulation
operations easy. In this lesson you learn how to manipulate the ﬁle system to rename, move, or
delete ﬁles and directories. You also learn how to read or write a text ﬁle’s contents all at once
rather than using a
These classes are in the System.IO namespace so you can make using them in
your code easier by including the directive:
THE DRIVEINFO CLASS
The DriveInfo class provides information about the system’s drives. Its static GetDrives
function returns an array of
DriveInfo objects describing all of the system’s drives.
Table 30-1 summarizes the
DriveInfo’s most useful properties.
The total number of bytes available.
The drive format, as in NTFS or FAT32.
596906c30.indd 349 4/7/10 12:34:24 PM
LESSON 30 Using File system Classes
The drive type, as in Fixed or CDRom.
True if the drive is ready. A drive must be ready before you can
AvailableFreeSpace, DriveFormat, TotalSize, or
The drive’s name, as in C:\.
A DirectoryInfo object representing the drive’s root directory.
The number of bytes available, taking quotas into account.
The drive’s total size in bytes.
The drive’s label.
Example program ListDrives (found as part of this lesson’s code download and shown in
Figure 30-1) uses the following code to describe the system’s drives.
// List the available drives.
private void Form1_Load(object sender, EventArgs e)
drivesListBox.DataSource = DriveInfo.GetDrives();
// Display information about the selected drive.
private void drivesListBox_SelectedIndexChanged(object sender, EventArgs e)
DriveInfo info = (DriveInfo)drivesListBox.SelectedItem;
nameTextBox.Text = info.Name;
rootTextBox.Text = info.RootDirectory.FullName;
typeTextBox.Text = info.DriveType.ToString();
isReadyTextBox.Text = info.IsReady.ToString();
596906c30.indd 350 4/7/10 12:34:24 PM
The DirectoryInfo Class
// See if the drive is ready.
// Display values.
labelTextBox.Text = info.VolumeLabel;
totalSpaceTextBox.Text = info.TotalSize.ToString();
totalFreeTextBox.Text = info.TotalFreeSpace.ToString();
availableFreeTextBox.Text = info.AvailableFreeSpace.ToString();
formatTextBox.Text = info.DriveFormat;
// Clear values that are unavailable.
THE DIRECTORYINFO CLASS
The DirectoryInfo class provides information about directories. Table 30-2 summarizes useful
DirectoryInfo methods for manipulating directories.
Creates a new directory. To use this, make a DirectoryInfo object,
passing its constructor the name of the directory to create. Then call
Creates a subdirectory inside this directory.
Deletes the directory. If you pass no parameters to this method, it
only deletes the directory if it is empty. Alternatively you can pass it
a Boolean parameter indicating whether you want to delete all of the
directory’s ﬁles and subdirectories.
Returns the directory’s immediate subdirectories. Optionally you can
include a search string to select particular subdirectories.
Returns the directory’s ﬁles. Optionally you can include a search string
to select particular ﬁles.
Moves the directory to a new path.
596906c30.indd 351 4/7/10 12:34:25 PM
LESSON 30 Using File system Classes
The DirectoryInfo class also provides a few useful properties, which are summarized in Table 30-3.
The directory’s attributes, such as Compressed, Hidden, or System.
The time at which the directory was created.
Returns True if the directory actually exists.
Gives the directory’s fully qualiﬁed path.
The time at which the directory was last accessed.
The time at which the directory was last written.
The directory’s name without the path.
A DirectoryInfo representing this directory’s parent directory.
The directory’s ﬁle system root.
Example program UseDirectoryInfo (found in this lesson’s code download) uses a DirectoryInfo
object to display information about directories.
THE DIRECTORY CLASS
The Directory class provides static methods for manipulating directories (see Table 30-4). For
simple tasks these are sometimes easier to use than the comparable
DirectoryInfo class methods
because you don’t need to create a
DirectoryInfo object to use them.
Creates the directory and any missing directories in its path up to the root.
Deletes a directory.
Returns true if the directory exists.
Returns the time at which the ﬁle was created.
Returns a directory’s subdirectories.
Returns the directory’s root.
Returns a directory’s ﬁles, optionally looking for ﬁles matching a pattern.
Returns the time at which a directory was last accessed.
596906c30.indd 352 4/7/10 12:34:25 PM