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
 
 
 
 
 
TitleGet disk drive information in VB .NET using the FileSystemObject
Keywordsdisk, drive, list
CategoriesTips and Tricks
 
This example uses the FileSystemObject. To use this object, the program needs a reference to the Microsoft Scripting Runtime library. Select the Project menu's Add Reference command and click on the COM tab. Select the Microsoft Scripting Runtime entry and click OK.

The code creates a FileSystemObject and iterates through the items in its Drives collection. For each drive, the program displays information from the corresponding Drive object.

Note that trying to access some of these values raises an error if the drive is not ready (for example, if a floppy drive is empty).

 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Dim fso As New Scripting.FileSystemObject()
    Dim drv As Scripting.Drive
    Dim string_builder As New System.Text.StringBuilder()

    For Each drv In fso.Drives
        string_builder.Append( _
            drv.DriveLetter & ":" & vbCrLf & _
            "  Type:            " & _
                drv.DriveType.ToString() & vbCrLf)
        If drv.IsReady Then
            string_builder.Append( _
                "  File System:     " & drv.FileSystem & _
                    vbCrLf & _
                "  Free Space:      " & drv.FreeSpace & _
                    vbCrLf & _
                "  Total Size:      " & drv.TotalSize & _
                    vbCrLf & _
                "  Volume Name:     " & drv.VolumeName & _
                    vbCrLf & _
                "  Serial Number:   " & drv.SerialNumber & _
                    vbCrLf & _
                "--------------------" & vbCrLf)
        Else
            string_builder.Append( _
                "  Not ready" & vbCrLf & _
                "--------------------" & vbCrLf)
        End If
    Next drv

    txtDrives.Text = string_builder.ToString()
    txtDrives.Select(0, 0)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated