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 drive information (type of drive, whether a floppy is present, etc.)
Description
Keywordsdisk, drive, drive information, serial number, volume name, file system type, FAT32, FAT, removable, loaded
CategoriesAPI, Windows
 
Use the GetVolumeInformation and GetDriveType API functions.

Thanks to Phil and Craig for information on this.

If you know how to pull other information out of these routines, let me know.

 
Private Sub cmdGetInfo_Click()
Dim volume_name As String
Dim file_system_name As String
Dim info_status As Long
Dim serial_number As Long
Dim max_component_length As Long
Dim file_system_flags As Long
Dim drive_name As String
Dim drive_type As Long
Dim txt As String
Dim pos As Integer

    txtInfo.Text = ""
    Screen.MousePointer = vbHourglass
    DoEvents

    volume_name = Space(256)
    file_system_name = Space(256)

    drive_name = drvInfo.Drive
    pos = InStr(drive_name, ":")
    If pos > 0 Then drive_name = Left$(drive_name, pos)
    If Right$(drive_name, 1) <> "\" Then drive_name = _
        drive_name & "\"
    drive_type = GetDriveType(drive_name)

    info_status = GetVolumeInformation(drive_name, _
        volume_name, Len(volume_name), serial_number, _
        max_component_length, file_system_flags, _
        file_system_name, Len(file_system_name))

     volume_name = CleanString(volume_name)
     file_system_name = CleanString(file_system_name)
 
    txt = _
        "Drive Name:" & vbTab & drive_name & vbCrLf & _
        "Volume Name:" & vbTab & "'" & volume_name & "'" & _
            vbCrLf & _
        "Serial Number:" & vbTab & serial_number & vbCrLf & _
            _
        "Max Component Length:" & vbTab & _
            max_component_length
    If max_component_length = 255 Then
        txt = txt & " (supports long file names)"
    End If
    txt = txt & vbCrLf & _
        "File System Flags:" & vbTab & file_system_flags & _
            vbCrLf

    If file_system_flags And FILE_CASE_PRESERVED_NAMES Then
        txt = txt & vbTab & "Preserves Names" & vbCrLf
    End If
    If file_system_flags And FILE_CASE_SENSITIVE_SEARCH Then
        txt = txt & vbTab & "Case Sensitive Search" & vbCrLf
    End If
    If file_system_flags And FILE_UNICODE_ON_DISK Then
        txt = txt & vbTab & "Unicode On Disk" & vbCrLf
    End If
    If file_system_flags And FILE_PERSISTENT_ACLS Then
        txt = txt & vbTab & "Persistent ACLS" & vbCrLf
    End If
    If file_system_flags And FILE_FILE_COMPRESSION Then
        txt = txt & vbTab & "File Compression" & vbCrLf
    End If
    If file_system_flags And FILE_VOLUME_IS_COMPRESSED Then
        txt = txt & vbTab & "Volumne Is Compressed" & vbCrLf
    End If
    If file_system_flags And FILE_SUPPORTS_ENCRYPTION Then
        txt = txt & vbTab & "Supports Encryption" & vbCrLf
    End If
    If file_system_flags And FILE_SUPPORTS_OBJECT_IDS Then
        txt = txt & vbTab & "Supports Object IDs" & vbCrLf
    End If
    If file_system_flags And FILE_SUPPORTS_REPARSE_POINTS _
        Then
        txt = txt & vbTab & "Supports Reparse Points" & _
            vbCrLf
    End If
    If file_system_flags And FILE_SUPPORTS_SPARSE_FILES Then
        txt = txt & vbTab & "Supports Sparse Files" & vbCrLf
    End If
    If file_system_flags And FILE_VOLUME_QUOTAS Then
        txt = txt & vbTab & "Volume Quotas" & vbCrLf
    End If

    txt = txt & _
        "File System Name:" & vbTab & "'" & _
            file_system_name & "'" & vbCrLf & _
        "Drive Type" & vbTab

    Select Case drive_type
        Case DRIVE_UNKNOWN
            txt = txt & "Unknown"
        Case DRIVE_NO_ROOT_DIR
            txt = txt & "No Root Dir"
        Case DRIVE_REMOVABLE
            txt = txt & "Removable"
            If info_status = 0 Then
                txt = txt & " (empty)"
            Else
                txt = txt & " (loaded)"
            End If
        Case DRIVE_FIXED
            txt = txt & "Fixed"
        Case DRIVE_REMOTE
            txt = txt & "Remote"
        Case DRIVE_CDROM
            txt = txt & "CD ROM"
            If info_status = 0 Then
                txt = txt & " (empty)"
            Else
                txt = txt & " (loaded)"
            End If
        Case DRIVE_RAMDISK
            txt = txt & "Ram Disk"
        Case Else
            txt = txt & "Error (" & Format$(drive_type) & _
                ")"
    End Select
    txt = txt & vbCrLf

    txtInfo.Text = txt
    Screen.MousePointer = vbDefault
End Sub

' Truncate the string at a NUll character if it
' contains one and remove leading and trailing spaces.
Private Function CleanString(ByVal txt As String) As String
Dim pos As Integer

    pos = InStr(txt, vbNullChar)
    If pos > 0 Then txt = Left$(txt, pos - 1)
    CleanString = Trim$(txt)
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated