Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDisplay drive information in Visual Basic 2005
DescriptionThis example shows how to display drive information in Visual Basic 2005.
Keywordsdrive information, VB2005, VB.NET
CategoriesWindows, Software Engineering, VB.NET
 
When the program starts, it fills a list box with the names of the system's drives.
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    For Each drive_info As DriveInfo In _
        DriveInfo.GetDrives()
        lstDrives.Items.Add(drive_info.Name)
    Next drive_info
End Sub
 
When you select a drive, the following code displays information about the drive. It clears its result labels. It then gets a DriveInfo object representing the drive and displays its information.
 
Private Sub lstDrives_SelectedIndexChanged(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    lstDrives.SelectedIndexChanged
    For Each ctl As Control In Me.Controls
        If (ctl.Name.StartsWith("lbl")) AndAlso (TypeOf ctl _
            Is Label) Then
            Dim lbl As Label = DirectCast(ctl, Label)
            lbl.Text = ""
        End If
    Next ctl

    Dim drive_info As New DriveInfo(lstDrives.Text)
    lblName.Text = drive_info.Name()
    lblIsReady.Text = drive_info.IsReady().ToString
    lblDriveType.Text = drive_info.DriveType().ToString
    lblRootDirectory.Text = _
        drive_info.RootDirectory.ToString
    If drive_info.IsReady() Then
        lblAvailableFreeSpace.Text = _
            drive_info.AvailableFreeSpace().ToString
        lblDriveFormat.Text = drive_info.DriveFormat()
        lblTotalFreeSpace.Text = _
            FormatBytes(drive_info.TotalFreeSpace, "0.00")
        lblVolumeLabel.Text = drive_info.VolumeLabel()
    End If
End Sub
 
The FormatBytes function displays a number of bytes in KB, MB, GB, and so forth.
 
' Format a disk size in bytes as KB, MB, GB, etc.
Private Function FormatBytes(ByVal num_bytes As Double, _
    ByVal format_str As String) As String
    Dim postfixes() As String = {"Bytes", "KB", "MB", "GB", _
        "TB", "PB", "EB", "ZB", "YB"}
    For i As Integer = postfixes.Length - 1 To 0 Step -1
        If num_bytes > 1024 ^ i Then
            num_bytes /= 1024 ^ i
            Return num_bytes.ToString(format_str) & " " & _
                postfixes(i)
        End If
    Next i

    Return num_bytes.ToString(format_str) & " Bytes"
End Function
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated