15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

eading Drive information ❘ 801<br />

using System;<br />

using System.IO;<br />

using System.Windows.Forms;<br />

namespace DriveViewer<br />

{<br />

public partial class Form1: Form<br />

{<br />

public Form1()<br />

{<br />

InitializeComponent();<br />

}<br />

private void Form1_Load(object sender, EventArgs e)<br />

{<br />

DriveInfo[] di = DriveInfo.GetDrives();<br />

}<br />

foreach (DriveInfo itemDrive in di)<br />

{<br />

listBox1.Items.Add(itemDrive.Name);<br />

}<br />

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)<br />

{<br />

DriveInfo di = new DriveInfo(listBox1.SelectedItem.ToString());<br />

}<br />

}<br />

}<br />

MessageBox.Show("Available Free Space: "<br />

+ di.AvailableFreeSpace + "\n" +<br />

"Drive Format: " + di.DriveFormat + "\n" +<br />

"Drive Type: " + di.DriveType + "\n" +<br />

"Is Ready: " + di.IsReady + "\n" +<br />

"Name: " + di.Name + "\n" +<br />

"Root Directory: " + di.RootDirectory + "\n" +<br />

"ToString() Value: " + di + "\n" +<br />

"Total Free Space: " + di.TotalFreeSpace + "\n" +<br />

"Total Size: " + di.TotalSize + "\n" +<br />

"Volume Label: " + di.VolumeLabel, di.Name +<br />

" DRIVE INFO");<br />

code download DriveViewer.sln<br />

The first step is to bring in the System.IO namespace with the using<br />

keyword. Within the Form1_Load event, you use the DriveInfo class<br />

to get a list of all the available drives on the system. This is done using<br />

an array of DriveInfo objects <strong>and</strong> populating this array with the<br />

DriveInfo.GetDrives() method. Then using a foreach loop, you are<br />

able to iterate through each drive found <strong>and</strong> populate the list box with<br />

the results. This produces something similar to what is shown in<br />

Figure 29-14.<br />

This form allows the end user to select one of the drives in the list.<br />

After a drive is selected, a message box appears that contains details<br />

about that drive. As you can see in Figure 29-14, I have six drives on<br />

my current computer. Selecting a couple of these drives produces the<br />

message boxes collectively shown in Figure 29-15.<br />

figure 29-14<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!