Recipe12.26.Querying Information for All Drives on a System


Recipe 12.26. Querying Information for All Drives on a System

Problem

Your application needs to know if a drive (HDD, CD drive, DVD drive, etc.) is available and ready to be written to and/or read from. Additionally, it would be nice to know if you have enough available free space on the drive to write information to.

Solution

Use the various properties in the DriveInfo class as shown here:

 public static void DisplayAllDriveInfo() {     foreach (DriveInfo drive in DriveInfo.GetDrives())     {         if (drive.IsReady)         {             Console.WriteLine("Drive " + drive.Name + " is ready.");             Console.WriteLine("AvailableFreeSpace: " + drive.AvailableFreeSpace);             Console.WriteLine("DriveFormat: " + drive.DriveFormat);             Console.WriteLine("DriveType: " + drive.DriveType);             Console.WriteLine("Name: " + drive.Name);             Console.WriteLine("RootDirectory.FullName: " +                 drive.RootDirectory.FullName);             Console.WriteLine("TotalFreeSpace: " + drive.TotalFreeSpace);             Console.WriteLine("TotalSize: " + drive.TotalSize);             Console.WriteLine("VolumeLabel: " + drive.VolumeLabel);     }     else     {             Console.WriteLine("Drive " + drive.Name + " is not ready.");     } } } 

This code will display something like the following, though of course each system is different and the results will vary:

 Drive C:\ is ready. AvailableFreeSpace: 143210795008 DriveFormat: NTFS DriveType: Fixed Name: C:\ RootDirectory.FullName: C:\ TotalFreeSpace: 143210795008 TotalSize: 159989886976 VolumeLabel: Vol1 Drive D:\ is ready. AvailableFreeSpace: 0 DriveFormat: UDF DriveType: CDRom Name: D:\ RootDirectory.FullName: D:\ TotalFreeSpace: 0 TotalSize: 3305965568 VolumeLabel: Vol2 Drive E:\ is ready. AvailableFreeSpace: 4649025536 DriveFormat: UDF DriveType: CDRom Name: E:\ RootDirectory.FullName: E:\ TotalFreeSpace: 4649025536 TotalSize: 4691197952 VolumeLabel: Vol3 Drive F:\ is not ready. 

Of particular interest are the IsReady and AvailableFreeSpace properties. The IsReady property determines if the drive is ready to be queried, written to, or read from. The AvailableFreeSpace property returns the free space on that drive in bytes.

Discussion

The DriveInfo class has been added to the .NET Framework to allow you to easily query information on one particular drive or on all drives in the system. To query the information from a single drive you would use the code in Example 12-15.

Example 12-15. Getting information from a specific drive

 DriveInfo drive = new DriveInfo("D"); if (drive.IsReady)     Console.WriteLine("The space available on the D:\\ drive: " +                       drive.AvailableFreeSpace); else     Console.WriteLine("Drive D:\\ is not ready."); 

Notice that only the drive letter is passed in to the Driveinfo constructor. This drive letter can be either uppercase or lowercaseit does not matter. The next thing you will notice with the code in Example 12-15 and the code in the Solution to this recipe is that the IsReady property is always tested for true before either using the drive or querying its properties. If we did not test this property for true and for some reason the drive was not ready (e.g., a CD was not in the drive at that time), a System.IO.IOException would be returned stating that "The device is not ready." To prevent this exception from being thrown (since it is an expensive operation), simply test the IsReady property to determine if it is true or not.

For the Solution to this recipe, the DriveInfo constructor was not used. Instead, the static GeTDrives method of the DriveInfo class was used to return an array of DriveInfo objects. Each DriveInfo object in this array corresponds to one drive on the current system.

The DriveType property of the DriveInfo class returns an enumeration value from the DriveType enumeration (yes, they have the same name, unfortunately). This enumeration value identifies what type of drive the current DriveInfo object represents. Table 12-13 identifies the various values of the DriveType enumeration.

Table 12-12. DriveType enumeration values

Enum value

Description

CDRom

This can be a CD-ROM, CD writer, DVD-ROM, or DVD writer drive.

Fixed

This is the fixed drive such as an HDD. Note that USB HDDs fall into this category.

Network

A network drive.

NoRootDirectory

No root directory was found on this drive.

Ram

A RAM disk.

Removable

A removable storage device.

Unknown

Some other type of drive than those listed here.


In the DriveInfo class there are two very similar properties, AvailableFreeSpace and TotalFreeSpace. Each of these properties will return the same value in most cases. However, AvailableFreeSpace also takes into account any disk-quota information for a particular drive. Disk-quota information can be found by right-clicking a drive in Windows Explorer and selecting the Properties pop-up menu item. This displays the Properties page for this drive. On this Properties page, click on the Quota tab to view the quota information for that drive. If the Enable Quota Management checkbox is unchecked, then disk-quota management is disabled, and both the AvailableFreeSpace and TotalFreeSpace properties should be equal.

See Also

See the "DriveInfo Class" topic in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net