13.1. Directories

Anytime I want to explore a new area in C#, the first thing I do is find out which namespaces are involved with that area. Since we're going to discuss file input and output (I/O), I simply typed in using System. at the top of the program and started looking for namespaces that may apply. Sure enough, up popped System.IO. Just highlight IO and press the F1 key and you'll get information about the IO namespace. All kinds of interesting things come up, but let's concentrate first on those classes that deal with directory and drive information.

13.1.1. The DriveInfo Class

Just as Ed McMahon used to say on the Johnny Carson show, "Everything you could ever possibly want to know . . . " about a disk drive is in the DriveInfo class. A few of the more useful methods and properties are presented in Table 13-1.

Table 13-1. Table 13-1
Method or PropertyDescription
GetDrives()Returns an array with all of the logical drives of the computer
AvailableFreeSpaceGives the amount of free disk space (in bytes) on a drive
DriveFormatReturns the format for the drive (such as NTFS or FAT32)
DriveTypeReturns the type of drive (such as fixed, removable, RAM, etc.)
NameGives the name of the drive
TotalFreeSpaceGives the amount of unused space on the drive
TotalSizeGives the capacity of the drive

If you look in the IO namespace, you will also discover that there are classes named Directory and DirectoryInfo. These two classes present related, albeit different, information.

13.1.2. Directory ...

Get Beginning C# 3.0 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.