How to get the Name & Type text of a drive as shown in My Computer

Dear Experts,

I have made a simple drive listing program. I use GetDriveType, VolID for the volume label and an answer from EE  to return a drives bus type to identify the drives.

However is it possible to obtain the same information for drives that Windows shows in My Computer (in Details view)?

I am mainly interested in the Name that Windows displays, and less importantly the Type.
For example, I have a USB ZIP drive, which I can only detect as a USB device - I can't distinguish between it and a USB Flash or Thumb drive.

Windows gives its name as "Iomega Zip 250 (H:)" which would solve my problem.

A  function, (preferably in Delphi) to return the Name and optionally Type of  a given drive letter would be great.
(assuming Windows makes this info publicly available)

Thanks!
WinRatAsked:
Who is Participating?
 
danaseamanCommented:
Use API SHGetFileInfoA to retrieve Display Name as shown is Windows Explorer.
Vb6 Exanple:

Option Explicit
 
Private Const MAX_PATH = 260
Private Const SHGFI_DISPLAYNAME = &H200
 
Private Type SHFILEINFOA
   hIcon                As Long
   iIcon                As Long
   dwAttributes         As Long
   szDisplayName        As String * MAX_PATH
   szTypeName           As String * 80
End Type
 
Private Declare Function SHGetFileInfoA Lib "shell32.dll" (ByVal pszPath As String, ByVal dwAttributes As Long, psfi As SHFILEINFOA, ByVal cbSizeFileInfo As Long, ByVal uFlags As Long) As Long
Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
 
Private Sub Form_Load()
   LoadDrives
End Sub
 
Public Sub LoadDrives()
   Dim DrvBitMask       As Long
   Dim MaxPwr           As Integer
   Dim Pwr              As Integer
   Dim MyDrive          As String
   Dim sDisplayName     As String
   Dim siA              As SHFILEINFOA
 
   On Error GoTo ProcedureError
 
   '-----
   DrvBitMask = GetLogicalDrives()
   If DrvBitMask Then
      MaxPwr = Int(Log(DrvBitMask) / Log(2))
      For Pwr = 0 To MaxPwr
         If 2 ^ Pwr And DrvBitMask Then
            MyDrive = Chr$(65 + Pwr) & ":\"
            'Get Drive DisplayName(as in Explorer).
            SHGetFileInfoA MyDrive, 0&, siA, Len(siA), SHGFI_DISPLAYNAME
            sDisplayName = StripNull(siA.szDisplayName)
            Debug.Print sDisplayName
         End If
      Next
   End If
   '-----
   Refresh
 
ProcedureExit:
   Exit Sub
ProcedureError:
End Sub
 
Public Function StripNull(StrIn As String) As String
   Dim nul              As Long
   nul = InStr(StrIn, vbNullChar)
   If (nul) Then
      StripNull = Left$(StrIn, nul - 1)
   Else
      StripNull = Trim$(StrIn)
   End If
End Function

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
danaseamanCommented:
To include Drive TypeName:

Option Explicit
 
Private Const MAX_PATH = 260
Private Const SHGFI_DISPLAYNAME = &H200
Private Const SHGFI_TYPENAME As Long = &H400
 
Private Type SHFILEINFOA
   hIcon                As Long
   iIcon                As Long
   dwAttributes         As Long
   szDisplayName        As String * MAX_PATH
   szTypeName           As String * 80
End Type
 
Private Declare Function SHGetFileInfoA Lib "shell32.dll" (ByVal pszPath As String, ByVal dwAttributes As Long, psfi As SHFILEINFOA, ByVal cbSizeFileInfo As Long, ByVal uFlags As Long) As Long
Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
 
Private Sub Form_Load()
   LoadDrives
End Sub
 
Public Sub LoadDrives()
   Dim DrvBitMask       As Long
   Dim MaxPwr           As Integer
   Dim Pwr              As Integer
   Dim MyDrive          As String
   Dim sDisplayName     As String
   Dim sTypeName        As String
   Dim siA              As SHFILEINFOA
 
   On Error GoTo ProcedureError
 
   '-----
   DrvBitMask = GetLogicalDrives()
   If DrvBitMask Then
      MaxPwr = Int(Log(DrvBitMask) / Log(2))
      For Pwr = 0 To MaxPwr
         If 2 ^ Pwr And DrvBitMask Then
            MyDrive = Chr$(65 + Pwr) & ":\"
            'Get Drive DisplayName(as in Explorer).
            SHGetFileInfoA MyDrive, 0&, siA, Len(siA), SHGFI_DISPLAYNAME Or SHGFI_TYPENAME
            sDisplayName = StripNull(siA.szDisplayName)
            sTypeName = StripNull(siA.szTypeName)
            Debug.Print sDisplayName, sTypeName
         End If
      Next
   End If
   '-----
   Refresh
 
ProcedureExit:
   Exit Sub
ProcedureError:
End Sub
 
Public Function StripNull(StrIn As String) As String
   Dim nul              As Long
   nul = InStr(StrIn, vbNullChar)
   If (nul) Then
      StripNull = Left$(StrIn, nul - 1)
   Else
      StripNull = Trim$(StrIn)
   End If
End Function

Open in new window

0
 
WinRatAuthor Commented:
Hi Everyone,

Thanks for the help so far.
I think I was a little premature with my question!
I have done some further testing and I see I was a little misled.

The Name strings shown in Explorer for the drives are one of three things:

1) Either the volume label assigned to a hard drive OR
2) The volume label assigned to CD or DVD media.
   (floppies don't show the discs volume label - always "3.5 Floppy")
3) A default string where 1 & 2 don't apply.

Examples on my PC:
DVD ROM drive  shows "DVD Dive"        [no media]
CD drive       shows "CD Drive"        [no media]
DVD-RAM        shows "DVD-RAM Drive"    [no media]
D Drive           shows "Local Disc"       [no voume label assigned]
USB ZIP drive  shows "Iomega Zip 250"   [no media]

I already read in the Volume Label in my program, but if none is assigned
I just get a null string. So being able to get the Windows default string
as in (3) above will be an improvement.

I will check out the example code and links tonight. In the case of the VB
example, I only know a little Excel VB-Script, so it may take a while to translate
to Delphi.
danaseaman: If you know Delphi would you be able to translate our examples for me?

Probably I should ask a separate question on how to detect a USB ZIP drive
as opposed to a normal USB drive.


0
 
WinRatAuthor Commented:
Sorry, have not been able to get to looking at your answers yet! Tomorrow.

0
 
WinRatAuthor Commented:
danaseaman:
Thank you for your help!
The example at the Delphi3000 link you gave was all I needed to understand how to use SHGetFileInfoA in my own program.
0
All Courses

From novice to tech pro — start learning today.