We help IT Professionals succeed at work.

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

Medium Priority
612 Views
Last Modified: 2013-11-25
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!
Comment
Watch Question

Dana SeamanDana Seaman (danaseaman)

Commented:
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

Dana SeamanDana Seaman (danaseaman)

Commented:
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

Author

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.


Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.