Section 5.1. Get Drive Information


5.1. Get Drive Information

.NET includes handy DirectoryInfo and FileInfo classes for gathering information about files and directories. However, in .NET 1.x there wasn't any way to get a list of all the drives on your computer without using unmanaged calls to the Windows API. Thankfully, the DriveInfo class finally debuts in .NET 2.0.


Note: The DriveInfo class lets you easily retrieve information about the drives on your computer.

Tip: Before going any further, start by importing the System.IO namespace. The file access classes in .NET 2.0 (FileInfo, DirInfo, and DriveInfo) all exist there. Whether you access them directly or through the My object, you'll need to import this namespace.

5.1.1. How do I do that?

The My.Computer.FileSystem provides a quick way to get a list of all the drives on the current computer. All you need to do is loop through the Drives collection, which exposes a collection of System.IO.DriveInfo objects.

For example, to see all the drive letters on the current computer, enter the following code:

' Display a list of drives. For Each Drive As DriveInfo In My.Computer.FileSystem.Drives     Console.WriteLine(Drive.Name) Next

This writes a list of drive letter names ("A:\", "C:\", "D:\", and so on). You can also create a DriveInfo object for a specific directory by using the My.Computer.FileSystem.GetDriveInfo( ) method, and specifying the letter of the drive you want to examine as a string. For example, try the following code to take a closer look at drive C:

Console.WriteLine("The label for drive C:\ is " & _   My.Computer.FileSystem.GetDriveInfo("C").VolumeLabel

This example displays the volume label that's set for the drive. You can also examine other properties of the DriveInfo object to get more information (such as the DriveType, TotalFreeSpace, and TotalSize). But keep in mind that you can't retrieve this information for a removable drive if there's no media present (for example, if the CD or diskette isn't in the drive). To guard against this possibility, check to make sure the DriveType doesn't include DriveType.Fixed before trying to get more detail.

Example 5-1 puts these concepts together with a complete, simple console application that displays information about all the drives on your computer.

Example 5-1. Displaying information about all the drives on a computer
Imports System.IO      Module DriveInfoTest          Public Sub Main( )         Console.WriteLine("Drives on this computer: ")         For Each Drive As DriveInfo In My.Computer.FileSystem.Drives             ' Display drive information.             Console.WriteLine(Drive.Name)                  Console.WriteLine(vbTab & "Type: " & Drive.DriveType.ToString( ))                  If (Drive.DriveType And DriveType.Fixed) = DriveType.Fixed Then                 Console.WriteLine(vbTab & "Format: " & _                   Drive.DriveFormat.ToString( ))                 Console.WriteLine(vbTab & "Label: " & Drive.VolumeLabel)                 Console.WriteLine(vbTab & "Total Size: " & Drive.TotalSize)                 Console.WriteLine(vbTab & "Free Space: " & Drive.TotalFreeSpace)             End If             Console.WriteLine( )         Next     End Sub      End Module

When you run this code, you'll see the following output:

Drives on this computer: A:\         Type: Removable      C:\         Type: Fixed         Format: NTFS         Label: Applications         Total Size: 15726702592         Free Space: 2788483072      D:\ ...

5.1.2. What about...

...getting information about the rest of the filesystem? The .NET Framework has always made it easy to get directory and file information using DirectoryInfo and FileInfo objects. Once you have instantiated a DriveInfo object, you can use its RootDirectory property to get a DirectoryInfo object that wraps the root directory (e.g., C:\). You can then use methods like DirectoryInfo.GetFiles( ) and DirectoryInfo.GetDirectories( ) to retrieve the files and subdirectories contained in the root directory.

5.1.3. Where can I learn more?

For more information about all the properties of the filesystem information classes, look for the "DriveInfo," "DirectoryInfo," and "FileInfo" index entries in the MSDN class library reference. You can also refer to other labs in this chapter, which show new shortcuts available with the My.Computer.FileSystem object. These include:

  • "Get File and Directory Information," which shows how you can quickly get information about a specific file or directory without directly creating a FileInfo or DirectoryInfo object.

  • "Copy, Move, and Delete Files," which shows how you can easily shuffle files and directories from one place to another.

  • "Read and Write Files," which shows the quickest way to extract text content from a file or write to a file.



Visual Basic 2005(c) A Developer's Notebook
Visual Basic 2005: A Developers Notebook
ISBN: 0596007264
EAN: 2147483647
Year: 2006
Pages: 123

Similar book on Amazon

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