Chapter 32. Using File System Classes

The techniques described in Lesson 31 enable you to 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 won't enable you to manipulate the file system itself, however. You can read from or write to a file using them, but not 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.

Note

These classes are in the System.IO namespace, so you can make using them in your code easier by including the following directive:

Imports 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 32-1 summarizes the DriveInfo's most useful properties.

Table 32.1. TABLE 32-1

PROPERTY

PURPOSE

AvailableFreeSpace

The total number of bytes available

DriveFormat

The drive format, as in NTFS or FAT32

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 ...

Get Stephens' Visual Basic® Programming 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.