Using File System Classes
The techniques described in Lesson 29 let you read and write text files. They also demonstrate
techniques that you’ll find useful when you deal with streams other than text files.
Those techniques dont let you manipulate the file system itself, however. They let you read or
write a file, but they don’t let you rename a file, move a file to a different directory, or delete a file.
This lesson describes file system classes that make these and other common file manipulation
operations easy. In this lesson you learn how to manipulate the file system to rename, move, or
delete files and directories. You also learn how to read or write a text file’s contents all at once
rather than using a
StreamReader.
These classes are in the System.IO namespace so you can make using them in
your code easier by including the directive:
using System.IO;
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
DriveInfos most useful properties.
TABLE 301
PROPERTY PURPOSE
AvailableFreeSpace
The total number of bytes available.
DriveFormat
The drive format, as in NTFS or FAT32.
30
continues
596906c30.indd 349 4/7/10 12:34:24 PM
350
LESSON 30 Using File system Classes
PROPERTY PURPOSE
DriveType
The drive type, as in Fixed or CDRom.
IsReady
True if the drive is ready. A drive must be ready before you can
use the
AvailableFreeSpace, DriveFormat, TotalSize, or
VolumeLabel properties.
Name
The drive’s name, as in C:\.
RootDirectory
A DirectoryInfo object representing the drive’s root directory.
TotalFreeSpace
The number of bytes available, taking quotas into account.
TotalSize
The drive’s total size in bytes.
VolumeLabel
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 systems drives.
FIGURE 301
// 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();
TABLE 301
(continued)
596906c30.indd 350 4/7/10 12:34:24 PM
The DirectoryInfo Class
351
// See if the drive is ready.
if (info.IsReady)
{
// 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;
}
else
{
// Clear values that are unavailable.
labelTextBox.Clear();
totalSpaceTextBox.Clear();
totalFreeTextBox.Clear();
availableFreeTextBox.Clear();
formatTextBox.Clear();
}
}
THE DIRECTORYINFO CLASS
The DirectoryInfo class provides information about directories. Table 30-2 summarizes useful
DirectoryInfo methods for manipulating directories.
TABLE 302
METHOD PURPOSE
Create
Creates a new directory. To use this, make a DirectoryInfo object,
passing its constructor the name of the directory to create. Then call
the
Create method.
CreateSubdirectory
Creates a subdirectory inside this directory.
Delete
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 files and subdirectories.
GetDirectories
Returns the directory’s immediate subdirectories. Optionally you can
include a search string to select particular subdirectories.
GetFiles
Returns the directory’s files. Optionally you can include a search string
to select particular files.
MoveTo
Moves the directory to a new path.
596906c30.indd 351 4/7/10 12:34:25 PM
352
LESSON 30 Using File system Classes
The DirectoryInfo class also provides a few useful properties, which are summarized in Table 30-3.
TABLE 303
PROPERTY PURPOSE
Attributes
The directory’s attributes, such as Compressed, Hidden, or System.
CreationTime
The time at which the directory was created.
Exists
Returns True if the directory actually exists.
FullName
Gives the directory’s fully qualified path.
LastAccessTime
The time at which the directory was last accessed.
LastWriteTime
The time at which the directory was last written.
Name
The directory’s name without the path.
Parent
A DirectoryInfo representing this directory’s parent directory.
Root
The directory’s file 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.
TABLE 304
METHOD PURPOSE
CreateDirectory
Creates the directory and any missing directories in its path up to the root.
Delete
Deletes a directory.
Exists
Returns true if the directory exists.
GetCreationTime
Returns the time at which the file was created.
GetDirectories
Returns a directory’s subdirectories.
GetDirectoryRoot
Returns the directory’s root.
GetFiles
Returns a directory’s files, optionally looking for files matching a pattern.
GetLastAccessTime
Returns the time at which a directory was last accessed.
596906c30.indd 352 4/7/10 12:34:25 PM

Get Stephens' C# Programming with Visual Studio® 2010 24-Hour Trainer now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.