Reading Drive Information


In addition to working with files and directories, the .NET Framework 2.0 introduces the ability to read information from a specified drive. This is done using the DriveInfo class. The DriveInfo class can do a scan of a system to provide a list of available drives and then can dig in deeper, providing you with tons of details about any of the drives.

For an example of using the DriveInfo class, create a simple Windows Form that will list out all the available drives on a computer and then will provide details on a user-selected drive. Your Windows Form will consist of a simple ListBox and should look as illustrated in Figure 24-13.

image from book
Figure 24-13

Once you have the form all set, the code will consist of two events - one for when the form loads and another for when the end user makes a drive selection in the list box. The code for this form is shown here:

  using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; namespace DriveInfo {     partial class Form1 : Form     {         public Form1()         {             InitializeComponent();         }         private void Form1_Load(object sender, EventArgs e)         {             DriveInfo[] di = DriveInfo.GetDrives();             foreach (DriveInfo itemDrive in di)             {                 listBox1.Items.Add(itemDrive.Name);             }         }         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)         {             DriveInfo di = new DriveInfo(listBox1.SelectedItem.ToString());             MessageBox.Show("Available Free Space: " + di.AvailableFreeSpace + "\n" +                 "Drive Format: " + di.DriveFormat + "\n" +                 "Drive Type: " + di.DriveType + "\n" +                 "Is Ready: " + di.IsReady.ToString() + "\n" +                 "Name: " + di.Name + "\n" +                 "Root Directory: " + di.RootDirectory + "\n" +                 "ToString() Value: " + di.ToString() + "\n" +                 "Total Free Space: " + di.TotalFreeSpace + "\n" +                 "Total Size: " + di.TotalSize + "\n" +                 "Volume Label: " + di.VolumeLabel.ToString(), di.Name +                 " DRIVE INFO");         }     } } 

The first step is to bring in the System.IO namespace with the using keyword. Within the Form1_Load event, you use the DriveInfo class to get a list of all the available drives on the system. This is done using an array of DriveInfo objects and populating this array with the DriveInfo.GetDrives() method.

Then using a foreach loop, you are able to iterate through each drive found and populate the list box with the results. This produces something similar to what is shown in Figure 24-14.

image from book
Figure 24-14

This form allows the end user to select one of the drives in the list. Once a drive is selected, a message box appears that contains details about that drive. As you can see in Figure 24-12, I have three drives on my current computer. Selecting each drive produces the message boxes collectively shown in Figure 24-15.

image from book
Figure 24-15

From here, you can see that these message boxes provide details about three entirely different drives. The first, drive C:\, is my hard drive, as the message box shows its drive type as Fixed. The second drive, drive D:\, is my CD/DVD drive. Here, you can even see the title of the DVD that I am viewing. The third drive, drive E:\, is my USB pen and is labeled with a drive type of Removable.




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

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