Recipe 12.1. Enumerating Drives


Problem

You need access to the list of drives available on the local workstation.

Solution

Sample code folder: Chapter 12\EnumerateDrives

Use the My.Computer.FileSystem.Drives collection to enumerate through the logical drives.

Discussion

If you have a form (Form1) with a ListBox control (ListBox1), the following code adds the name of each available drive to the list when the form first opens:

 Private Sub Form1_Load(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles MyBase.Load    For Each oneDrive As IO.  DriveInfo In _          My.Computer.  FileSystem.Drives       ListBox1.Items.Add(oneDrive)    Next oneDrive End Sub 

That code adds complete objects of type System.IO.DriveInfo to the list. If you only want to add the drive names, use this code instead:

 Private Sub Form1_Load(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles MyBase.Load    For Each oneDrive As IO.DriveInfo In _          My.Computer.FileSystem.Drives       ListBox1.Items.Add(oneDrive.Name)    Next oneDrive End Sub 

Each added item appears as X:\, where X is replaced by the drive letter. Figure 12-1 shows the output of this code on a computer with just a "C" drive.

Figure 12-1. The list of drives on a typical one-drive workstation


The My.Computer.FileSystem.Drives collection provides access to details about each local or network drive attached to the workstation. Since it is a collection that exposes the IEnumerable interface, you can use it in a For Each statement, accessing each drive object in the collection.

The System.IO.DriveInfo object includes the following useful properties:


AvailableFreeSpace

Returns the number of free bytes available to the current user on the drive. If the administrator has instituted disk quotas on the drive, this amount may be considerably less than the total available space on the drive.


DriveFormat

Returns a string indicating the file-system type. Common file systems available in Windows include NTFS, FAT, FAT32, and CDFS.


DriveType

Indicates the type of drive through the System.IO.DriveType enumeration. The most common drive types include Fixed, Removable, and CDRom.


IsReady

Returns a Boolean that indicates whether the drive is ready for use. Typically, this flag is accessed on CD drives to determine whether a CD is in the drive and ready to use.


Name

Gets the name of a drive, in X:\ format.


RootDirectory

Returns a System.IO.DirectoryInfo object that refers to the top-most directory of the drive. This property will not be valid if the DriveType property is set to NoRootDirectory.


TotalFreeSpace

Returns the number of free bytes available on the drive. Unlike the AvailableFreeSpace property, this property is not limited by any administrator defined quotas.


TotalSize

Returns the total number of used and unused bytes on the drive. This property is not limited by any administrator-defined quotas.


VolumeLabel

Indicates the volume label currently assigned to the drive. If the drive supports it, you can modify the volume label by assigning a new String value to this property. Some drives impose length limits on the volume label.

See Also

Recipe 12.8 shows how to iterate directories within a drive.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon

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